user352156
user352156

Reputation: 99

Replace special characters in vbscript

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:

  1. check whether the whole string contains a special character in it
  2. If yes, replace these characters with a "?"

I haven't tried anything yet since i'm a newbie in vb scripting.

Upvotes: 3

Views: 20784

Answers (3)

JanTheGun
JanTheGun

Reputation: 2223

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

Upvotes: 6

Pradnya Bolli
Pradnya Bolli

Reputation: 1943

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

Upvotes: 1

Dave
Dave

Reputation: 977

Try something like this:

strCur="!@#$%^&*()?><~`+=|\/.',{}[];:-%_20"

for iCount = 0 to len(strCur )
     paragraph= Replace(paragraph, Mid(strCur, iCount + 1, 1), "?")
next

'This line should replace those characters. You'll need a line for each char.
paragraph = Replace$(paragraph, Chr(123), "a")
paragraph = Replace$(paragraph, Chr(173), "A")

Upvotes: 1

Related Questions