string VB.NET中+和&连接字符串的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/734600/
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
The difference between + and & for joining strings in VB.NET
提问by Rob P.
What is the difference between +
and &
for joining strings in VB.NET?
在 VB.NET 中连接字符串+
和&
用于连接字符串有什么区别?
回答by Kibbee
There's no difference if both operands are strings. However, if one operand is a string, and one is a number, then you run into problems, see the code below.
如果两个操作数都是字符串,则没有区别。但是,如果一个操作数是字符串,一个是数字,那么您就会遇到问题,请参阅下面的代码。
"abc" + "def" = "abcdef"
"abc" & "def" = "abcdef"
"111" + "222" = "111222"
"111" & "222" = "111222"
"111" & 222 = "111222"
"111" + 222 = 333
"abc" + 222 = conversion error
Therefore I recommend to always use &
when you mean to concatenate, because you might be trying to concatenate an integer, float, decimal to a string, which will cause an exception, or at best, not do what you probably want it to do.
因此,我建议&
在要连接时始终使用,因为您可能试图将整数、浮点数、十进制数连接到字符串,这将导致异常,或者充其量不执行您可能希望它执行的操作。
回答by Guffa
The & operator always makes sure that both operands are strings, while the + operator finds the overload that matches the operands.
& 运算符始终确保两个操作数都是字符串,而 + 运算符查找与操作数匹配的重载。
The expression 1 & 2
gives the value "12", while the expression 1 + 2 gives the value 3.
表达式1 & 2
给出值“12”,而表达式 1 + 2 给出值 3。
If both operands are strings, there is no difference in the result.
如果两个操作数都是字符串,则结果没有区别。
回答by Aliostad
None.
没有任何。
As you can see below. These two lines of code compiles exactly to the same CIL code:
正如你在下面看到的。这两行代码完全编译为相同的 CIL 代码:
Module Module1
Sub Main()
Dim s1 As String = "s1"
Dim s2 As String = "s2"
s2 += s1
s1 &= s2
End Sub
End Module
Compiles to (note System.String::Concat
):
编译为(注System.String::Concat
):
.method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
// Code size 31 (0x1f)
.maxstack 2
.locals init ([0] string s1,
[1] string s2)
IL_0000: nop
IL_0001: ldstr "s1"
IL_0006: stloc.0
IL_0007: ldstr "s2"
IL_000c: stloc.1
IL_000d: ldloc.1
IL_000e: ldloc.0
IL_000f: call string [mscorlib]System.String::Concat(string,
string)
IL_0014: stloc.1
IL_0015: ldloc.0
IL_0016: ldloc.1
IL_0017: call string [mscorlib]System.String::Concat(string,
string)
IL_001c: stloc.0
IL_001d: nop
IL_001e: ret
} // end of method Module1::Main
回答by dbasnett
The + operator can be either addition or concatenation. The & is only concatenation. If the expressions are both strings the results would be the same.
+ 运算符可以是加法或串联。& 只是连接。如果表达式都是字符串,则结果将相同。
I use & when working with strings, and + when working with numbers, so there is never confusion about my intent. If you mistakenly use + and one expression is a string and one is a number, you run the risk of un-desired results.
我在处理字符串时使用 & ,在处理数字时使用 + ,所以我的意图永远不会混淆。如果您错误地使用了 + 并且一个表达式是一个字符串而一个是一个数字,那么您将面临出现不想要的结果的风险。
回答by dr. evil
There is no difference in most of the cases. However, the best practice is:
大多数情况下没有区别。但是,最佳做法是:
"+" should be reserved for integer additions, because if you don't use Option Strict Onthen you might have really messed up situations such as:
"+" 应该保留给整数加法,因为如果你不使用Option Strict On那么你可能真的搞砸了,比如:
Input + 12
might give you 20
instead of 812
. This can be especially bad in an ASP.NET application where the input comes from POST/GET.
Input + 12
可能会给你20
而不是812
. 这在输入来自 POST/GET 的 ASP.NET 应用程序中尤其糟糕。
Simply put: For joining strings, always use "&" instead of "+".
简单地说:对于连接字符串,始终使用“&”而不是“+”。
Obviously, use StringBuilderwhere it's suitable :)
显然,在合适的地方使用StringBuilder:)
回答by JaredPar
If both of the types are statically typed to System.String, there is zero difference between the code. Both will resolve down to the String.Concatmember (this is what +
does for strings).
如果这两种类型都是静态类型为System.String,则代码之间的差异为零。两者都将解析为String.Concat成员(这就是+
字符串的作用)。
However, if the objects are not strongly typed to string, Visual Basic late binding will kick in and go two very different routes. The +
version will attempt to do an addoperation which literally tries to add the objects. This will do all manner of attempts to convert both values to a number and then add them.
但是,如果对象不是强类型为字符串,Visual Basic 后期绑定将启动并走两条截然不同的路线。该+
版本将尝试执行添加操作,该操作实际上尝试添加对象。这将进行各种尝试,将两个值都转换为一个数字,然后将它们相加。
The &
operator will attempt to concatenate. The Visual Basic runtime will go through all manner of conversions to convert both values to strings. It will then String.Concat
the results.
该&
运营商将试图串连。Visual Basic 运行时将通过各种转换方式将两个值都转换为字符串。然后就会String.Concat
有结果。
回答by FranciscoBouza
Straight from MSDN Documentation: Concatenation Operators in Visual Basic
直接来自 MSDN 文档:Visual Basic 中的连接运算符
Differences Between the Two Concatenation Operators
The + Operator (Visual Basic) has the primary purpose of adding two numbers. However, it can also concatenate numeric operands with string operands. The + operator has a complex set of rules that determine whether to add, concatenate, signal a compiler error, or throw a run-time InvalidCastException exception.
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) 的主要目的是将两个数字相加。但是,它也可以将数字操作数与字符串操作数连接起来。+ 运算符具有一组复杂的规则,用于确定是添加、连接、发出编译器错误信号还是抛出运行时 InvalidCastException 异常。
& 运算符 (Visual Basic) 仅针对 String 操作数定义,并且无论 Option Strict 的设置如何,它始终将其操作数扩展为 String。建议将 & 运算符用于字符串连接,因为它是专门为字符串定义的,并减少了生成意外转换的机会。
Do trust MSDN! :-)
请相信 MSDN!:-)
回答by Gavin Miller
None when joining strings:
加入字符串时无:
Dim string1 As String = "A" + "B"
Dim string2 As String = "A" & "B"
If string1.Equals(string2) And string2.Equals(string1) Then
Debugger.Break()
End If