Replace special characters in vbscript
14,589
Solution 1
You can use a regular expression where you add every character that you consider as a non-special character.
stringsToCheck = Array("Windows Live Fot¢t r", _
"Galer¡a fotogr fica de Windows Live", _
"Windows Live Maker")
Set regExp = New RegExp
regExp.IgnoreCase = True
regExp.Global = True
regExp.Pattern = "[^a-z0-9 !?@]" 'Add here every character you don't consider as special character
For each str In stringsToCheck
strProcessed = regExp.Replace(str, "?")
WScript.Echo "Old String: " & str
WScript.Echo "New String: " & strProcessed
Next
Output:
Old String: Windows Live Fot¢t r
New String: Windows Live Fot?t r
Old String: Galer¡a fotogr fica de Windows Live
New String: Galer?a fotogr fica de Windows Live
Old String: Windows Live Maker
New String: Windows Live Maker
Solution 2
You can try below code ..
Function replaceChars(str As String) As String
'msgbox replacechars ("!@#$%^&*(") will return !@$%^&()
Dim elem As Variant
replaceChars = str
For Each elem In Array("/", "\", ":", "*", "?", "<", ">", "|", "#", Chr(34))
replaceChars = Replace(replaceChars, elem, "?")
Next
End Function
Author by
user352156
Updated on June 13, 2022Comments
-
user352156 almost 2 years
I have a set of strings that may or may not have special characters in it. Example:
Windows Live Fot¢t r Galer¡a fotogr fica de Windows Live Windows Live Maker
What i wanna do is to:
- check whether the whole string contains a special character in it
- If yes, replace these characters with a "?"
I haven't tried anything yet since i'm a newbie in vb scripting.