58

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?

APerson
  • 7,304
  • 5
  • 32
  • 48
sushant
  • 895
  • 5
  • 17
  • 32

8 Answers8

91

You can escape by doubling the quotes

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

or write an explicit chr() call

g="abcd " & chr(34) & a & chr(34)
tanascius
  • 50,043
  • 21
  • 106
  • 130
16

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

g = "abcd """ & a & """"
Simon
  • 8,777
  • 4
  • 35
  • 53
8

The traditional way to specify quotes is to use Chr(34). This is error resistant and is not an abomination.

Chr(34) & "string" & Chr(34)
Wolf
  • 8,482
  • 7
  • 48
  • 92
David Candy
  • 739
  • 5
  • 8
8

I usually do this:

Const Q = """"

Dim a, g
a = "xyz"  
g = "abcd " & Q & a & Q

If you need to wrap strings in quotes more often in your code and find the above approach noisy or unreadable, you can also wrap it in a function:

a = "xyz"  
g = "abcd " & Q(a)

Function Q(s)
  Q = """" & s & """"
End Function
Tomalak
  • 306,836
  • 62
  • 485
  • 598
0

I don't think I can improve on these answers as I've used them all, but my preference is declaring a constant and using that as it can be a real pain if you have a long string and try to accommodate with the correct number of quotes and make a mistake. ;)

0

I designed a simple approach using single quotes when forming the strings and then calling a function that replaces single quotes with double quotes.

Of course this approach works as long as you don't need to include actual single quotes inside your string.

Function Q(s)

    Q = Replace(s,"'","""")

End Function

...

user="myself"
code ="70234"
level ="C"

r="{'User':'" & user & "','Code':'" & code & "','Level':'" & level & "'}"
r = Q(r)
response.write r

...

Hope this helps.

J. Scott Elblein
  • 3,179
  • 11
  • 44
  • 72
0

You can do like:

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

Or:

a=chr(34) & "xyz" & chr(34)
g="abcd " & a  
Sarfraz
  • 355,543
  • 70
  • 511
  • 562
-2

I found the answer to use double and triple quotation marks unsatisfactory. I used a nested DO...LOOP to write an ASP segment of code. There are repeated quotation marks within the string. When I ran the code:

thestring = "<asp:RectangleHotSpot Bottom=""" & bottom & """ HotSpotMode=""PostBack"" Left="""& left & """    PostBackValue=""" &xx & "." & yy & """ Right=""" & right & """ Top=""" & top & """/>"

the output was: <`asp:RectangleHotSpot Bottom="28

 'Changing the code to the explicit chr() call worked:

thestring = "<asp:RectangleHotSpot Bottom=""" & bottom & chr(34) & " HotSpotMode=""PostBack"" Left="""& left & chr(34) & " PostBackValue=""" &xx & "." & yy & chr(34) & " Right=""" & right & chr(34) & " Top=""" & top & chr(34) &"/>"

The output:

<asp:RectangleHotSpot Bottom="28" HotSpotMode="PostBack" Left="0" PostBackValue="0.0" Right="29" Top="0"/>
tckmn
  • 52,184
  • 22
  • 101
  • 145
Russell S
  • 95
  • 4
  • 1
    chr(34) works. But so does escaping the double quotes. Your top example code would not output what you claim it did; something else was wrong there. Your "working" example even includes escaping, proving that it does work. – Andrew Barber Jul 29 '13 at 01:40
  • The numbers in the output code were from the values assigned to the variables. This code is just a snippet. The whole output was a 178 KB file. Double and triple quotes do work, but not for what I needed. I don't begin to know why, but that doesn't change the fact that it didn't work as it should have in my case. I was writing the output to a file. It could be that multiple escapes inside nested loops using one interpreted code to write another while writing to a file was too much for the VBscript interpreter. – Russell S Jul 29 '13 at 04:05