Remove Special Character from String to form a Filename

Remove Special Character from String to form a Filename

A.Acheson
Mentor Mentor
1,150 Views
3 Replies
Message 1 of 4

Remove Special Character from String to form a Filename

A.Acheson
Mentor
Mentor

I am looking to remove the quote symbol ["] used as inch units in a system generated name before using it as a filename.

 

Dim Name As String
Name = "BW-S-(100)4""
NewName = Name.Replace(""", "")
MessageBox.Show("Name", "Title")
MessageBox.Show("NewName", "Title")

Is it possible? What would the method need to be? 

 

I have tried the string function Replace but """ is not correct.  

Any help greatly appreciated.

Thanks.

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Accepted solutions (2)
1,151 Views
3 Replies
Replies (3)
Message 2 of 4

etaCAD
Advocate
Advocate
Accepted solution

Maybe your are looking for something like this:

 Dim Name As String
 Name = "BW-S-(100)4"""
 Dim NewName As String = Name.Replace("""", "")
 MessageBox.Show(Name, "Title")
 MessageBox.Show(NewName, "Title")

Andreas
etaCAD

Message 3 of 4

A.Acheson
Mentor
Mentor

@etaCAD 

Thanks for that. 

 

I looked at the rest of the potential strings and found some other invalid characters. I found this helpful function to do that. 

https://stackoverflow.com/questions/45458954/replace-invalid-characters-when-saving-excel-as-pdf

Sub Main
 Dim Name As String
 Name = "BW-S-(100)4"""
 Dim replace_with As String
 replace_with = ""
 Dim NewName As String = clean_filename(Name, replace_with)
 MessageBox.Show(Name, "Title")
 MessageBox.Show(NewName, "Title")
End Sub



Function clean_filename(Name As String, replace_with As String)
    Dim inv_chars As String
    Dim cpos As Long
    invchars = "'!""#¤%&/()=?`^*>;:@£${[]}|~\,.'¨´+-"
    For cpos = 1 To Len(invchars)
        Name = Replace(Name, Mid(invchars, cpos, 1), replace_with)
    Next
    clean_filename = Name
	'Return clean_filename
End Function

 

If this solved a problem, please click (accept) as solution.‌‌‌‌
Or if this helped you, please, click (like)‌‌
Regards
Alan
0 Likes
Message 4 of 4

NachoShaw
Advisor
Advisor
Accepted solution

you can also simplify it

 

Dim Name As String = Replace("BW-S-(100)4""", Chr(34), "")

 

Nacho
Automation & Design Engineer

Inventor automation Programmer (C#, VB.Net / iLogic)
Furniture, Sheet Metal, Structural, Metal fab, Tradeshow, Fabrication, CNC

EESignature


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.