472,353 Members | 1,886 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 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 5996
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...
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...
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...
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...
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...
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...
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...
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...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

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.