About using double quotes in Vbscript

I have a very basic doubt about vb scripts:

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 whether I need to save (in a variable) or the double-quoted display string, why do I need to use the doble word twice for a word or phrase. Is the use of common double quotes used, does not mean that I want to display the whole thing, or can be saved as a string in a variable?

+9
source share
4 answers

In VBScript, string literals are surrounded by double quotes ( "). Here is what your first example shows:

Msgbox "This is myName" ' This works fine

, , , VBScript , . :

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 "

, -. , , VBScript , " -". , escape- VBScript . :

Msgbox "This is ""myName"""   'This works fine
  • , .
  • , . : .
  • .
  • , .

(\) escape-. . , VBScript escape, , :

Msgbox "This is \"myName\""   ' doesn't work in VBScript; example only

, :

Const Quote = """"

' ... later in the code ...

Msgbox "This is " & Quote & "myName" & Quote
+17

escape-. , VB/VBS . , . Tab, .

escape- 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
+3

VBScript . qoute , , . - , . 3 rd , 2 nd .

+1

, , , . VBScript - "( ). " , "( ) .

To contain a separator in a string literal, it must be escaped (marked as not signifying the "end" or "beginning" of the string. The escape marker for "in string literals": "provision" is in VBscript. Other languages ​​are used \"to avoid double quotation marks.

So

Msgbox "This is ""myName"""   'This works fine
x = "This is ""myName"""

correct VBScript if you want to display (or save) This is "myName".

+1
source

All Articles