在 VBScript 中向字符串添加引号
我有这样的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以通过加倍引号
或编写显式的
chr()
调用来转义You can escape by doubling the quotes
or write an explicit
chr()
call您必须使用双双引号来转义双引号(笑):
You have to use double double quotes to escape the double quotes (lol):
我通常这样做:
如果您需要在代码中更频繁地将字符串括在引号中,并且发现上述方法嘈杂或不可读,您也可以将其包装在函数中:
I usually do this:
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:
指定引号的传统方法是使用
Chr(34)
。这是防错的并且不是令人厌恶的。The traditional way to specify quotes is to use
Chr(34)
. This is error resistant and is not an abomination.你可以这样做:
或者:
You can do like:
Or:
我不认为我可以改进这些答案,因为我已经使用了它们,但我更喜欢声明一个常量并使用它,因为如果你有一个很长的字符串并尝试适应正确的数字,这可能会很痛苦引用并犯错误。 ;)
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. ;)
我设计了一种简单的方法,在形成字符串时使用单引号,然后调用用双引号替换单引号的函数。
当然,只要您不需要在字符串中包含实际的单引号,这种方法就有效。
......
希望
这有帮助。
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.
...
...
Hope this helps.
我发现使用双引号和三引号的答案并不令人满意。我使用嵌套的 DO...LOOP 来编写一段 ASP 代码。字符串内有重复的引号。当我运行代码时:
输出是:
<`asp:RectangleHotSpot Bottom="28
输出:
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:
the output was:
<`asp:RectangleHotSpot Bottom="28
The output: