3

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.

user692942
  • 16,398
  • 7
  • 76
  • 175
user352156
  • 99
  • 1
  • 4
  • 14

3 Answers3

6

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
JanTheGun
  • 2,165
  • 17
  • 25
1

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
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
0

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")
Dave
  • 967
  • 10
  • 23