I have a very basic doubt in vb scripting:
Msgbox "This is myName" ' This works fine
Msgbox "This is "myName"" ' This gives an error
Msgbox "This is ""myName""" 'This works fine
My question is if I need to save (in a variable) or display string with double quotes why I need to use a doble quotes twice for the word or phrase. Does using a common double quotes doesn't mean I want to display the entire thing or could be saved as string in variable?
In VBScript, string literals are surrounded by double quotes ("
). This is what your first example shows:
Msgbox "This is myName" ' This works fine
However, if you want to include a double quote character inside of your string literal, you've got a problem, because VBScript is going to interpret the second double quote character it finds as signifying the end of the string literal. This is what your second example shows:
Msgbox "This is "myName"" ' This gives an error
^ ' because it prematurely terminates the string here
' and doesn't know what to do with the trailing "
Fortunately, there's an escape hatch. It involves escaping the double quote character with another character, indicating that VBScript should process it as a literal double quote character, rather than a magical "end-of-string-literal" character. It just so happens that the escape character VBScript uses is a double quote character. This is what your second example shows:
Msgbox "This is ""myName""" 'This works fine
Other languages often use a backslash (\
) as the escape character. That might make things easier to see. Assuming VBScript used a backslash as the escape character rather than a double quote, your code would look like this:
Msgbox "This is \"myName\"" ' doesn't work in VBScript; example only
If this syntax bothers you, you can declare a constant for the double quote and use that each time:
Const Quote = """"
' ... later in the code ...
Msgbox "This is " & Quote & "myName" & Quote
Each language has its own escape character. By chance or not, in VB/VBS it is double quote-mark. And also by chance or not, we can embed only double quote in literal string. We cannot embed other special characters as Tab for example.
However, using VB/VBS escape character simplify our coding.
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"
But personally I prefer using Replace
as it make my code more readable.
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"
You can use Replace
(if that convenient to you) and for the rest special characters.
str = Replace(Replace("A|B|C!1|2|3", "!", vbNewLine), "|", vbTab)
WScript.Echo str
'A B C
'1 2 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With