473,327 Members | 2,025 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,327 software developers and data experts.

StringBuilder.Append() vs concatenation

Hi folks,

I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.

Example A
---------
SringBuilder sb = new StringBuilder();
sb.Append("Some text");
sb.Append(" and some more text");

is more efficient than

Example B
---------
string myString = "Some text";
myString += " some more text";

But what about this.

Example C
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = " + myVar + " and that's that.");

vs.

Example D
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = ");
sb.Append(myVar);
sb.Append(" and that's that.");

I'm thinking that the concatenation in the Example C does not have the
same overhead as the reassignment in Example B and therefore Example D
is overkill, but I'm prepared to be corrected.

Cheers

Simon
Dec 3 '05 #1
6 6044
sb.Append("Variable = " + myVar + " and that's that.");
has concatenation the operation inside brackets is the same as Example B

so the most efficient will be Example D.

--
Vadym Stetsyak aka Vadmyst
http://vadmyst.blogspot.com

"Simon" <si****************@nodomain.com> wrote in message
news:ev****************@TK2MSFTNGP10.phx.gbl...
Hi folks,

I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.

Example A
---------
SringBuilder sb = new StringBuilder();
sb.Append("Some text");
sb.Append(" and some more text");

is more efficient than

Example B
---------
string myString = "Some text";
myString += " some more text";

But what about this.

Example C
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = " + myVar + " and that's that.");

vs.

Example D
---------
StringBuilder sb = new StringBuilder();
sb.Append("Variable = ");
sb.Append(myVar);
sb.Append(" and that's that.");

I'm thinking that the concatenation in the Example C does not have the
same overhead as the reassignment in Example B and therefore Example D is
overkill, but I'm prepared to be corrected.

Cheers

Simon

Dec 3 '05 #2
Simon <si****************@nodomain.com> wrote:
I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.


<snip>

See http://www.pobox.com/~skeet/csharp/stringbuilder.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 3 '05 #3
Ok,

I remember reading some time ago that

var = "This" + " is" + " a" + " test";

does not have same issues as

var = "This";
var += " is";
var += " a";
var += " test";

Hence my question. I can see where your coming from on a conceptual
level and how it would reduce down to the same tokens, just confused as
I had read the contrary before.

I take it that this was wrong.

Cheers

Simon
Vadym Stetsyak wrote:
sb.Append("Variable = " + myVar + " and that's that.");
has concatenation the operation inside brackets is the same as Example B

so the most efficient will be Example D.

Dec 3 '05 #4
Thanks Jon,

Very clear article.

Many thanks

Simon

Jon Skeet [C# MVP] wrote:
Simon <si****************@nodomain.com> wrote:
I'm aware of the fact that using a StringBuilder is more efficient than
constantly reassigning a string. For example.

<snip>

See http://www.pobox.com/~skeet/csharp/stringbuilder.html

Dec 3 '05 #5
Simon <si****************@nodomain.com> wrote:
I remember reading some time ago that

var = "This" + " is" + " a" + " test";

does not have same issues as

var = "This";
var += " is";
var += " a";
var += " test";


Well, there are two issues there.

1) The first is actually a constant, so it becomes exactly equivalent
to:

var = "This is a test";

2) Even with variables, it would be one concatenation call, without
intermediate strings being constructed.

See http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 3 '05 #6
Yup I get ya Jon. Not the best example of what I was thinking. I've read
the article. All is clear.

Many thanks

Simon

Jon Skeet [C# MVP] wrote:
Simon <si****************@nodomain.com> wrote:
I remember reading some time ago that

var = "This" + " is" + " a" + " test";

does not have same issues as

var = "This";
var += " is";
var += " a";
var += " test";

Well, there are two issues there.

1) The first is actually a constant, so it becomes exactly equivalent
to:

var = "This is a test";

2) Even with variables, it would be one concatenation call, without
intermediate strings being constructed.

See http://www.pobox.com/~skeet/csharp/stringbuilder.html for more
information.

Dec 4 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
8
by: | last post by:
Hi! I'm very interesting in when to use exactly the StringBuilder? For example for something like this?: String strTest1 = "This"; String strTest2 = "Test"; StringBuilder stbTest = new...
16
by: Alvin Bruney | last post by:
Is string builder intelligent enough to handle concats without behaving like string? Consider myStringBuilder.Append("one" + "two") what does the '+' do here? Because this syntax is also...
4
by: Ron | last post by:
Hello, I have to concatenate some large strings which end up in a text file. I am just checking if the StringBuilder class can improve what I am currently doing - and how to implement this. ...
2
by: Ron | last post by:
Hello, I have to read the contents of a textfile and would like to read it row by row. Each row contains about 180 fields. Most of the data is varchar, but some text fields (memo fields). So...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
12
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a...
10
by: =?Utf-8?B?RGFyYSBQ?= | last post by:
Can some one suggest me as why StringBuilder class is better than Strings? -- Never say, Give up
5
by: pantagruel | last post by:
Hi, It is generally stated that stringbuilder should be used instead of just concatenating strings with the plus operator. That's fine enough what I'm wondering in cases I have: String S =...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.