VBA:&和+ [英] VBA: Difference between & and +
问题描述
两者之间有什么区别
string1 + string2
和
string1 & string2
它们等效吗?为什么会有两个不同的符号执行相同的操作?
Are they equivalent? Why have two different symbols that do the same thing?
推荐答案
只要操作数是字符串,表达式是相同的;如果不是,则+
可能会添加它们,具体取决于类型转换. &
保证除了字符串连接外不会得到任何东西,并且会尽可能将操作数转换为字符串.
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.
有一个有关 Visual Basic中的串联操作的MSDN条目对此进行了解释:
There's an MSDN entry about Concatenation operations in Visual Basic that explains it:
&运算符(Visual Basic)仅为String操作数定义,并且无论Option Strict的设置如何,它始终将其操作数扩展为String. &建议您将运算符用于字符串连接,因为该操作符是专门为字符串定义的,可减少生成意外转换的机会.
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.
这篇关于VBA:&和+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!