在 VB.Net 中通过 & 和 + 连接字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4670247/
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
Concat strings by & and + in VB.Net
提问by Javed Akram
Is there any difference between & and + operators while concatenating string? if yes, then what is difference? And if No, then why below code generating exception?
连接字符串时 & 和 + 运算符之间有什么区别吗?如果是,那么有什么区别?如果否,那么为什么下面的代码会生成异常?
Example:
例子:
Dim s, s1, t As String
Dim i As Integer
s1 = "Hello"
i = 1
s = s1 & i
t = s1 + i //Exception here
If s = t Then
MessageBox.Show("Equal...")
End If
回答by N. Hodin
& and + are both concatenation operators but when you specify an integer while using +, vb.net tries to cast "Hello" into integer to do an addition. If you change "Hello" with "123", you will get the result 124.
& 和 + 都是连接运算符,但是当您在使用 + 时指定整数时,vb.net 会尝试将“Hello”转换为整数以进行加法。如果将“Hello”更改为“123”,则会得到结果 124。
回答by MarkJ
&
is only used for string concatenation.+
is overloaded to do both string concatenation and arithmetic addition.
&
仅用于字符串连接。+
被重载以进行字符串连接和算术加法。
The double purpose of +
leads to confusion, exactly like that in your question. Especially when Option Strict
is Off
, because the compiler will add implicit casts on your strings and integers to try to make sense of your code.
的双重目的+
导致混淆,就像你的问题一样。特别是当Option Strict
is 时Off
,因为编译器会在您的字符串和整数上添加隐式强制转换以试图理解您的代码。
My recommendations
我的建议
- You should definitely turn
Option Strict On
, then the compiler will force you to add explicit casts where it thinks they are necessary. - You should avoid using
+
for concatenation because of the ambiguity with arithmetic addition.
- 你绝对应该转
Option Strict On
,然后编译器会强迫你在它认为必要的地方添加显式强制转换。 +
由于算术加法的含糊不清,您应该避免使用for concatenation。
Both these recommendations are also in the Microsoft Press book Practical Guidelines And Best Practises for VB and C#(sections 1.16, 21.2)
这两项建议也在 Microsoft Press 的书Practical Guidelines And Best Practices for VB and C#(第 1.16、21.2 节)中
回答by Chris B. Behrens
You've probably got Option Strict turned on (which is a good thing), and the compiler is telling you that you can't add a string and an int. Try this:
您可能打开了 Option Strict(这是一件好事),并且编译器告诉您不能添加字符串和整数。尝试这个:
t = s1 & i.ToString()
t = s1 & i.ToString()
回答by Mark Hurd
As your question confirms, they are different: &
is ONLY string concatenation, +
is overloaded with both normal addition and concatenation.
正如您的问题所确认的那样,它们是不同的:&
仅是字符串连接,+
通过正常的加法和连接重载。
In your example:
在你的例子中:
because one of the operands to
+
is an integer VB attempts to convert the string to a integer, and as your string is not numeric it throws; and&
only works with strings so the integer is converted to a string.
因为操作数之一
+
是整数 VB 尝试将字符串转换为整数,并且由于您的字符串不是数字,所以它会抛出;和&
仅适用于字符串,因此整数被转换为字符串。
回答by Shubham Verma
You can write '&' to add string and integer :
您可以编写 '&' 来添加字符串和整数:
processDetails=objProcess.ProcessId & ":" & objProcess.name
message = msgbox(processDetails,16,"Details")
output will be:
输出将是:
5577:wscript.exe
回答by Ewan
Try this. It almost seemed to simple to be right. Simply convert the Integer to a string. Then you can use the method below or concatenate.
尝试这个。正确似乎很简单。只需将整数转换为字符串。然后你可以使用下面的方法或连接。
Dim I, J, K, L As Integer
Dim K1, L1 As String
K1 = K
L1 = L
Cells(2, 1) = K1 & " - uploaded"
Cells(3, 1) = L1 & " - expanded"
MsgBox "records uploaded " & K & " records expanded " & L
回答by Alex7575
My 2 cents:
我的 2 美分:
If you are concatenating a significant amount of strings, you should be using the StringBuilder instead. IMO it's cleaner, and significantly faster.
如果要连接大量字符串,则应改用 StringBuilder。IMO 它更干净,而且速度明显更快。
回答by Buck Hicks
From a former string concatenater (sp?) you should really consider using String.Format instead of concatenation.
从以前的字符串连接器 (sp?) 开始,您真的应该考虑使用 String.Format 而不是连接。
Dim s1 As String
Dim i As Integer
s1 = "Hello"
i = 1
String.Format("{0} {1}", s1, i)
It makes things a lot easier to read and maintain and I believe makes your code look more professional. See: code better – use string.format. Although not everyone agrees When is it better to use String.Format vs string concatenation?
它使事情更容易阅读和维护,我相信让你的代码看起来更专业。请参阅:更好地编写代码 - 使用 string.format。虽然不是每个人都同意什么时候使用 String.Format 与字符串连接更好?