Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding quotes to a string in VBScript

I have this code:

a = "xyz"   g = "abcd " & a   

After running it, the value of g is abcd xyz.

However, I want quotes around the value of a in g. After running the code, g should be abcd "xyz" instead.

How can I accomplish this?

like image 806
sushant Avatar asked May 31 '10 09:05

sushant


People also ask

How do I add a single quote to a string in VBA?

When you enter a single quotation mark, you must enter a second – VBA wants to see a pair of them. If you omit the quotation marks, VBA will interpret Harkins as a variable and then return an error when it doesn't find it (assuming of course that you haven't actually declared a variable named Harkins.)

How do you pass double quotes in VBScript?

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.

How do you concatenate values in VBScript?

The VBScript script concatenation operator is an ampersand "&" and occurs in between the two strings to be joined. This example will join a total of 4 strings to form a super string.

What is CHR 34 in VBScript?

In two of the arguments, you have opening quotation marks embedded in the string (that's what the Chr(34) represents), but no closing quote. While that's legal VBScript code, it probably won't work the way you intended.


2 Answers

You can escape by doubling the quotes

g="abcd """ & a & """" 

or write an explicit chr() call

g="abcd " & chr(34) & a & chr(34) 
like image 64
tanascius Avatar answered Oct 26 '22 06:10

tanascius


You have to use double double quotes to escape the double quotes (lol):

g = "abcd """ & a & """" 
like image 26
Simon Avatar answered Oct 26 '22 06:10

Simon