By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,876 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,876 IT Pros & Developers. It's quick & easy.

String.Concat

P: n/a
For simple string concatenation, is there a difference between...

Dim s As String
s += "add this to string"

....and...

Dim s As String
s = String.Concat(s, "add this to string")

....and...

Dim s As String
s = s + "add this string"

Do they result in the same CLR code? If not, is one faster or
better in some way than the others?

Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"John Ford" <zj**@C1h2e3z4F5o6r7d8.com> schrieb
For simple string concatenation, is there a difference between...

Dim s As String
s += "add this to string"

...and...

Dim s As String
s = String.Concat(s, "add this to string")

...and...

Dim s As String
s = s + "add this string"

Do they result in the same CLR code?
Yes
s = s & ...
and
s &=
also

If not, is one faster or
better in some way than the others?


No
--
Armin

Nov 20 '05 #2

P: n/a
John,
Do not use + for string concatenation, use & for string concatenation.

As + is the addition operator while & is the string concatenation operator.

If you use ILDASM.EXE you will see that &= compiles to a call to
String.Concat.

So yes they result in the same IL code, and the speed would be the same.

I normally use the operator as it is a 'cleaner' syntax.

Just remember depending on what you are doing '&=' in a loop for example. It
will be faster to create a System.Text.StringBuilder object and call the
Append method.

For example (VS.NET 2003 syntax)
Dim startTime, endTime As DateTime
Dim time As TimeSpan
Dim s As String
startTime = DateTime.Now
For i As Integer = 0 to 10000
s &= "12345678901234567890"
Next
endTime = DateTime.Now
time = endTime.Subtract(startTime)
Debug.WriteLine(time, "String")

startTime = DateTime.Now
Dim sb As New System.Text.StringBuilder
For i As Integer = 0 to 10000
sb.Append("12345678901234567890")
Next
s = sb.ToString()
endTime = DateTime.Now
time = endTime.Subtract(startTime)
Debug.WriteLine(time, "String")

You will find the first loop takes significantly longer than the second
loop, especially as you increase the number of iterations. The reason for
this is that &= creates a new string for each iteration, while the
StringBuilder maintains a buffer internally that is larger than the
resultant string, this buffer is doubled each time the StringBuilder needs
more room. Resulting in better memory management.

Hope this helps
Jay

"John Ford" <zj**@C1h2e3z4F5o6r7d8.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
For simple string concatenation, is there a difference between...

Dim s As String
s += "add this to string"

...and...

Dim s As String
s = String.Concat(s, "add this to string")

...and...

Dim s As String
s = s + "add this string"

Do they result in the same CLR code? If not, is one faster or
better in some way than the others?

Nov 20 '05 #3

P: n/a
Hello,

"John Ford" <zj**@C1h2e3z4F5o6r7d8.com> schrieb:
For simple string concatenation, is there a difference between...

Dim s As String
s += "add this to string"

...and...

Dim s As String
s = String.Concat(s, "add this to string")

...and...

Dim s As String
s = s + "add this string"

Do they result in the same CLR code? If not, is one faster or
better in some way than the others?


Have a look at the code using ildasm.exe.

Notice that it's recommended to use the & operator to concatenate strings in
VB .NET.

HTH,
Herfried K. Wagner
--
MVP VB Classic, VB .NET
http://www.mvps.org/dotnet
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.