替换VBScript中的特殊字符

3
我有一系列的字符串,可能包含特殊字符。 例如:
Windows Live Fot¢t r
Galer¡a fotogr fica de Windows Live
Windows Live Maker

我想做的是:

  1. 检查整个字符串中是否包含特殊字符
  2. 如果包含,将这些字符替换为"?"

由于我是vb脚本的新手,还没有尝试过任何方法。


2
首先,定义什么是特殊字符或非特殊字符,以及根据什么定义? - Kul-Tigin
2
你是在使用VBScript还是VB.Net?这两个是不同的东西。 - Blackwood
3个回答

6

您可以使用正则表达式,其中添加每个您认为非特殊字符的字符。

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

输出:

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

1
您可以尝试以下代码..

 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

0

可以尝试这样做:

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")

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接