每种语言都有自己的escape character。偶然与否,在 VB/VBS 中它是双引号。而且无论是否偶然,我们只能在文字字符串中嵌入双引号。例如,我们不能嵌入其他特殊字符作为 Tab。
但是,使用 VB/VBS 转义字符简化了我们的编码。
str = """D:\path\to\xyz.exe"" ""arg 1"" ""arg 2"""
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Chr(34) & "D:\path\to\xyz.exe" & Chr(34) & " " _
& Chr(34) & "arg 1" & Chr(34) & " " & Chr(34) & "arg 2" & Chr(34)
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
str = Join(Array("", "D:\path\to\xyz.exe", " ", "arg 1", " ", "arg 2", ""), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
但我个人更喜欢使用Replace,因为它使我的代码更具可读性。
str = Replace("'D:\path\to\xyz.exe' 'arg 1' 'arg 2'", Chr(39), Chr(34))
WScript.Echo str ' "D:\path\to\xyz.exe" "arg 1" "arg 2"
您可以使用Replace(如果方便的话)和其他特殊字符。
str = Replace(Replace("A|B|C!1|2|3", "!", vbNewLine), "|", vbTab)
WScript.Echo str
'A B C
'1 2 3