VBA:& 和 + 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3365197/
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
VBA: Difference between & and +
提问by l--''''''---------''''''''''''
What is the difference between:
有什么区别:
string1 + string2
and
和
string1 & string2
Are they equivalent? Why have two different symbols that do the same thing?
它们是等价的吗?为什么有两个不同的符号做同样的事情?
采纳答案by Michael Mrozek
The expressions are the same as long as the operands are strings; if not, +
might add them instead depending on type conversions. &
guarantees you won't get anything except a string concatenation, and will convert operands to strings if possible to do so.
只要操作数是字符串,表达式就相同;如果没有,+
可能会根据类型转换添加它们。&
保证除了字符串连接之外你不会得到任何东西,并且如果可能的话将操作数转换为字符串。
There's an MSDN entry about Concatenation operations in Visual Basicthat explains it:
有一个关于Visual Basic中的连接操作的 MSDN 条目解释了它:
The & Operator (Visual Basic) is defined only for String operands, and it always widens its operands to String, regardless of the setting of Option Strict. The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.
& 运算符 (Visual Basic) 仅针对 String 操作数定义,并且无论 Option Strict 的设置如何,它始终将其操作数扩展为 String。建议将 & 运算符用于字符串连接,因为它是专门为字符串定义的,并减少了生成意外转换的机会。
回答by Georg Fritzsche
The two expressions are equivalent, but the operators are not. +
can be used as an arithmetic operator as well as for string concatenation, &
can only be used for the latter.
这两个表达式是等价的,但运算符不是。+
可以用作算术运算符以及用于字符串连接,&
只能用于后者。