在 VBA 中的字符串中放置双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42960548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Placing Double Quotes Within a String in VBA
提问by coolDude
I'm having a hard time understanding how to place a double quote (") within a String in VBA. I know that I can easily do this using the char(34)
function. I also understand that another way of doing this is to use 4 double quotes: """"
. All of this comes from a previous SO post:
我很难理解如何在 VBA 中的字符串中放置双引号 (")。我知道我可以使用该char(34)
函数轻松地做到这一点。我也明白另一种方法是使用 4 个双引号: """"
. 所有这些都来自之前的 SO 帖子:
How do I put double quotes in a string in vba?
However, my question is.... Why are 4 quotes needed? Do the first two act as the escape, the third is the quote itself, and the fourth is the terminating quote? Or does it work in a different way? I haven't been able to find a concrete answer as to how VBA treats these double quotes.
但是,我的问题是.... 为什么需要 4 个引号?前两个是否充当转义符,第三个是引用本身,第四个是终止引用吗?或者它以不同的方式工作?我一直无法找到关于 VBA 如何处理这些双引号的具体答案。
I've also noticed that if I try adding or removing the number of double quotes within a String, Visual Studio will dynamically add or remove double quotes. For example, I initially had the following String:
我还注意到,如果我尝试在字符串中添加或删除双引号的数量,Visual Studio 将动态添加或删除双引号。例如,我最初有以下字符串:
data = TGName + """ + iterator.Value + """
...which produces the following within a message box:
...在消息框中生成以下内容:
However, if I try adjusting the second set of double quotes at the end of the String (+ """
) from 3 to 4, Visual Studio automatically adjusts this to 5. There's no way for me to only have 4 quotes at the end. This is the resulting String within a message box:
但是,如果我尝试将字符串 ( + """
)末尾的第二组双引号从 3 调整为 4,Visual Studio 会自动将其调整为 5。我无法在末尾只有 4 个引号。这是消息框中的结果字符串:
The Strings within the message boxes aren't the actual output that I'm hoping to have, they're purely for experimental purposes. However, what I've noticed is that there clearly is a requirement for the number of quotes that are allowed within a String in VBA. Does anyone know what that requirement is? Why is the IDE forcefully inserting an additional quote in the second String? Can someone explain the differences between the actual String contents and the formatting quotes within both cases that I've described?
消息框中的字符串不是我希望的实际输出,它们纯粹是为了实验目的。但是,我注意到的是,在 VBA 中,字符串中允许的引号数量显然是有要求的。有谁知道这个要求是什么?为什么 IDE 会在第二个字符串中强行插入额外的引号?有人可以解释我所描述的两种情况下实际字符串内容和格式引号之间的区别吗?
As always, any assistance on this would be greatly appreciated :)
与往常一样,对此的任何帮助将不胜感激:)
回答by A.S.H
The general rule is as follows.
一般规则如下。
The first double-quote (DQ) announces the beginning of a string. Afterwards, some DQ announces the end of the string. However, if a DQ is preceded by a DQ, it is "escaped". Escaped means it is a character part of the string, not a delimiter.
第一个双引号 (DQ) 表示字符串的开始。之后,一些 DQ 宣布字符串的结束。但是,如果 DQ 前面有 DQ,则它是“转义”。转义意味着它是字符串的字符部分,而不是分隔符。
Simply put, when you have any even numberof consecutive double-quotes inside a string, say 2n
, this means there are n escaped double-quotes. When the number is odd, say 2n+1
, you have n escaped DQs and a delimiter.
简单地说,当字符串中有任意偶数个连续双引号时,比如说2n
,这意味着有 n 个转义的双引号。例如,当数字为奇数时2n+1
,您有 n 个转义 DQ 和一个分隔符。
Examples
例子
""" + iterator.Value + """
' delimiter " + iterator.Value + " delimiter
' ^ escaped ^ escaped
""" + iterator.Value + """"
' delimiter " + iterator.Value + "" ' (missing enclosing delimiter)
' ^ escaped ^^ both escaped.
In this latter case the last delimiter is missing, For this reason VS inserted it for you, and you got 5 DQs.
在后一种情况下,缺少最后一个分隔符,因此 VS 为您插入了它,并且您得到了 5 个 DQ。
Finally the particular case """"
(just 4 DQs), the first and last are delimiters, and inside there's one escaped DQ. This is equivalent to chr(34)
.
最后是特殊情况""""
(只有 4 个 DQ),第一个和最后一个是分隔符,里面有一个转义的 DQ。这相当于chr(34)
.
回答by Gordon Bell
To append iterator value to TGName in quotes, you can do this:
要将迭代器值附加到引号中的 TGName,您可以执行以下操作:
Data = TGName & """" & iterator.Value & """"
or this:
或这个:
Data = TGName & Chr(34) & iterator.Value & Chr(34)
Note: I replaced + signs with & because that's simply a VBA best practice when concatenating strings.
注意:我用 & 替换了 + 符号,因为这只是连接字符串时的 VBA 最佳实践。