vb.net 替换 vbscript 中的特殊字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28853070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:51:10  来源:igfitidea点击:

Replace special characters in vbscript

vb.netstringvbscript

提问by user352156

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 "?"
  1. 检查整个字符串中是否包含特殊字符
  2. 如果是,请将这些字符替换为“?”

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

我还没有尝试过任何东西,因为我是 vb 脚本的新手。

回答by JanTheGun

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

回答by Pradnya Bolli

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

回答by Dave

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