我有一组字符串,可能有也可能没有特殊字符。 例如:
Windows Live Fot¢t r
Galer¡a fotogr fica de Windows Live
Windows Live Maker
我想做的是:
- 检查整个字符串是否包含特殊字符
- 如果是,请用"?"
替换这些字符 醇>
由于我是vb脚本中的新手,我还没有尝试过任何事情。
答案 0 :(得分:2)
您可以使用正则表达式添加您认为是非特殊字符的每个字符。
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 :(得分: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")
答案 2 :(得分:0)
您可以尝试以下代码..
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