473,382 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

String.Concat

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
3 11142
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Hector A | last post by:
Hi I'm trying to convert a string that already looks like a date to a date that I can use when I pass it from java to the database. I receive the date in format yyyy-mm-dd and I need it to be a...
4
by: Ereinion | last post by:
Hello. In C# you can do like this: int a = 5, b = 10; string s = "blabla bla" + a + "bla bla bla" + b + "."; How do you do that in mc++?
8
by: Doug Stiers | last post by:
Is there a downside to using string.concat? Other than a little overhead? str1 = string.concat(str1,str2) vs. str1 &= str2 It seems to me like the string class should be optimized to do this...
1
by: Trint Smith | last post by:
Ok, I have a webform that has these checkboxes: 1. something 2. something else 3. and something else When the user clicks on the checkbox, I want all of the selections to go into a textbox...
10
by: Ben | last post by:
Hi I have a string which is at least 4 characters but can be more which I need to pad, with zeros, to a full length of 15 after the 3rd charactor. Eg '1234' to '123000000000004' How can I...
3
by: Mythran | last post by:
Out of curiosity, only, which is recommended for SHORT concatenation...or concatenating two or three strings that are relatively small in size? Dim a As String = "bah" Dim b As String = "bah2"...
7
by: Leonel Gayard | last post by:
Hi all, I had to write a small script, and I did it in python instead of shell-script. My script takes some arguments from the command line, like this. import sys args = sys.argv if args ==...
27
by: user | last post by:
Have require file with several query stings in it. Depending on user input one of strings is selected. Everything going along smoothly until I wanted to also input a variable in string. If I put...
15
by: James | last post by:
Which is better, which is faster, which is easier etc... ????? String.Format ( "yadda {0} yadda {1}", x, y ) "yadda" + x.ToString() + " yadda" + y.tostring(); My code has a mish mash of...
2
by: Tony Johansson | last post by:
Hello! string object are immutable but if I have this kind of construction se below 1. will this be done so only one string object is created or will a new string object be created to concat...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.