问题
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
回答1:
& 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.
回答2:
&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.
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.
Both these recommendations are also in the Microsoft Press book Practical Guidelines And Best Practises for VB and C# (sections 1.16, 21.2)
回答3:
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:
t = s1 & i.ToString()
回答4:
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.
回答5:
You can write '&' to add string and integer :
processDetails=objProcess.ProcessId & ":" & objProcess.name
message = msgbox(processDetails,16,"Details")
output will be:
5577:wscript.exe
回答6:
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
回答7:
From a former string concatenater (sp?) you should really consider using String.Format instead of concatenation.
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?
回答8:
My 2 cents:
If you are concatenating a significant amount of strings, you should be using the StringBuilder instead. IMO it's cleaner, and significantly faster.
来源:https://stackoverflow.com/questions/4670247/concat-strings-by-and-in-vb-net