473,782 Members | 2,423 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11160
"John Ford" <zj**@C1h2e3z4F 5o6r7d8.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.Str ingBuilder 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 &= "12345678901234 567890"
Next
endTime = DateTime.Now
time = endTime.Subtrac t(startTime)
Debug.WriteLine (time, "String")

startTime = DateTime.Now
Dim sb As New System.Text.Str ingBuilder
For i As Integer = 0 to 10000
sb.Append("1234 567890123456789 0")
Next
s = sb.ToString()
endTime = DateTime.Now
time = endTime.Subtrac t(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**@C1h2e3z4F 5o6r7d8.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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**@C1h2e3z4F 5o6r7d8.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
39809
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 date variable in 'mm/dd/yyyy' or 'm/dd/yyyy' format. My code is shown below. Any suggestions? code: String sailYear = (String)sail_date.toString().substring(0,4);
4
1233
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
3529
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 functionality. Thanks in advance.
1
1924
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 if all are checked. But currently, just the last selection is going in. Here's the code I have:
10
2635
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 do this? Also how can I truncate this back to 4 or however many charactors (e.g.
3
3085
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" Dim c As String = a & b Dim d As String = String.Concat(a, b) string a = "bah"; string b = "bah2";
7
7583
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 == : print """Concat: concatenates the arguments with a colon (:) between them
27
10126
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 string in program works ok, but, if I use string from require file I can not seem to insert string. $cccb_id is sting..... to be inserted into $query4 and changes depending on user input.
15
2609
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 both and I am wondewring if I should standardize or not ??
2
1124
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 This and is and then another string object be created to concat Thisis and a and so on 1.
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.