Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About using Double quotes in Vbscript

Tags:

vbscript

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?

like image 499
user1925406 Avatar asked Apr 02 '13 17:04

user1925406


2 Answers

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
  • You begin the string with a single double-quote, indicating the start of a string literal.
  • Then you want to have an embedded double quote character, so you use two of them. This is where the escaping starts: you escape the double quote character with another double quote character.
  • Then you do that escaping thing again.
  • Finally, you terminate the entire string literal with another double quote character.

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
like image 73
Cody Gray Avatar answered Sep 26 '22 02:09

Cody Gray


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
like image 31
Panayot Karabakalov Avatar answered Sep 24 '22 02:09

Panayot Karabakalov