在 VBScript 中向字符串添加引号

发布于 2024-09-03 18:15:12 字数 253 浏览 4 评论 0原文

我有这样的代码:

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

运行后,g的值为abcd xyz

但是,我想要在 g 中为 a 的值加上引号。运行代码后,g 应改为 abcd "xyz"

我怎样才能做到这一点?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

一瞬间的火花 2024-09-10 18:15:12

您可以通过加倍引号

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

或编写显式的 chr() 调用来转义

g="abcd " & chr(34) & a & chr(34)

You can escape by doubling the quotes

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

or write an explicit chr() call

g="abcd " & chr(34) & a & chr(34)
奶气 2024-09-10 18:15:12

您必须使用双双引号来转义双引号(笑):

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

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

g = "abcd """ & a & """"
内心激荡 2024-09-10 18:15:12

我通常这样做:

Const Q = """"

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

如果您需要在代码中更频繁地将字符串括在引号中,并且发现上述方法嘈杂或不可读,您也可以将其包装在函数中:

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

Function Q(s)
  Q = """" & s & """"
End Function

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
浮生未歇 2024-09-10 18:15:12

指定引号的传统方法是使用 Chr(34)。这是防错的并且不是令人厌恶的。

Chr(34) & "string" & Chr(34)

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)
像极了他 2024-09-10 18:15:12

你可以这样做:

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

或者:

a=chr(34) & "xyz" & chr(34)
g="abcd " & a  

You can do like:

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

Or:

a=chr(34) & "xyz" & chr(34)
g="abcd " & a  
终弃我 2024-09-10 18:15:12

我不认为我可以改进这些答案,因为我已经使用了它们,但我更喜欢声明一个常量并使用它,因为如果你有一个很长的字符串并尝试适应正确的数字,这可能会很痛苦引用并犯错误。 ;)

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. ;)

﹏半生如梦愿梦如真 2024-09-10 18:15:12

我设计了一种简单的方法,在形成字符串时使用单引号,然后调用用双引号替换单引号的函数。

当然,只要您不需要在字符串中包含实际的单引号,这种方法就有效。

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

希望

这有帮助。

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.

故人的歌 2024-09-10 18:15:12

我发现使用双引号和三引号的答案并不令人满意。我使用嵌套的 DO...LOOP 来编写一段 ASP 代码。字符串内有重复的引号。当我运行代码时:

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

输出是:
<`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) &"/>"

输出:

<asp:RectangleHotSpot Bottom="28" HotSpotMode="PostBack" Left="0" PostBackValue="0.0" Right="29" Top="0"/>

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"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文