关于在 Vbscript 中使用双引号 [英] About using Double quotes in Vbscript

查看:93
本文介绍了关于在 Vbscript 中使用双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 vb 脚本有一个非常基本的疑问:

I have a very basic doubt in vb scripting:

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 if I need to save (in a variable) or display string with double quotes why I need to use a doble quotes twice for the word or phrase. Does using a common double quotes doesn't mean I want to display the entire thing or could be saved as string in variable?

推荐答案

在 VBScript 中,字符串文字被双引号 (") 括起来.这就是您的第一个示例:

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

Msgbox "This is myName" ' This works fine

但是,如果您想在字符串文字中包含一个双引号字符,就会遇到问题,因为 VBScript 会将它找到的第二个双引号字符解释为表示字符串文字的结尾.这是您的第二个示例显示的内容:

However, if you want to include a double quote character inside of your string literal, you've got a problem, because VBScript is going to interpret the second double quote character it finds as signifying the end of the string literal. This is what your second example shows:

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 应该将它作为文字双引号字符处理,而不是神奇的字符串结尾文字"字符.碰巧 VBScript 使用的转义字符是双引号字符.这是您的第二个示例显示的内容:

Fortunately, there's an escape hatch. It involves escaping the double quote character with another character, indicating that VBScript should process it as a literal double quote character, rather than a magical "end-of-string-literal" character. It just so happens that the escape character VBScript uses is a double quote character. This is what your second example shows:

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

  • 字符串以一个双引号开始,表示字符串文字的开始.
  • 然后你想有一个嵌入的双引号字符,所以你使用了其中的两个.这是转义开始的地方:您转义双引号字符与另一个双引号字符.
  • 然后你再做一次逃避的事情.
  • 最后,用另一个双引号字符终止整个字符串文字.
    • 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.
    • Then you do that escaping thing again.
    • Finally, you terminate the entire string literal with another double quote character.
    • 其他语言通常使用反斜杠 (\) 作为转义字符.这可能会使事情更容易看到.假设 VBScript 使用反斜杠作为转义字符而不是双引号,您的代码将如下所示:

      Other languages often use a backslash (\) as the escape character. That might make things easier to see. Assuming VBScript used a backslash as the escape character rather than a double quote, your code would look like this:

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

      如果这个语法困扰你,你可以为双引号声明一个常量并每次都使用它:

      If this syntax bothers you, you can declare a constant for the double quote and use that each time:

      Const Quote = """"
      
      ' ... later in the code ...
      
      Msgbox "This is " & Quote & "myName" & Quote
      

      这篇关于关于在 Vbscript 中使用双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆