469,647 Members | 1,794 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,647 developers. It's quick & easy.

Stringbuilder vs Strings

Can some one suggest me as why StringBuilder class is better than Strings?
--
Never say, Give up
May 9 '07 #1
10 4249
On May 9, 2:39 pm, Dara P <D...@discussions.microsoft.comwrote:
Can some one suggest me as why StringBuilder class is better than Strings?
See http://pobox.com/~skeet/csharp/stringbuilder.html

Jon

May 9 '07 #2
On May 9, 9:39 am, Dara P <D...@discussions.microsoft.comwrote:
Can some one suggest me as why StringBuilder class is better than Strings?
Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.

May 9 '07 #3
It should be said that you should use StringBuilder to concatenate when
you don't exactly know how many strings you are going to concatenate. The
best example is when you have N number of strings and you need to append it
all together in a loop.

If you have a known number of strings, then using the concatenation
operator will work just fine, as it will compile to a call to the static
Concat method on the string class, which is very fast (it pre-allocates the
return buffer, and then does a memory copy of the strings into the
appropriate locations in the return buffer).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...
On May 9, 9:39 am, Dara P <D...@discussions.microsoft.comwrote:
>Can some one suggest me as why StringBuilder class is better than
Strings?

Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.

May 9 '07 #4
On May 9, 6:02 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
It should be said that you should use StringBuilder to concatenate when
you don't exactly know how many strings you are going to concatenate. The
best example is when you have N number of strings and you need to append it
all together in a loop.

If you have a known number of strings, then using the concatenation
operator will work just fine, as it will compile to a call to the static
Concat method on the string class, which is very fast (it pre-allocates the
return buffer, and then does a memory copy of the strings into the
appropriate locations in the return buffer).

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"Andy" <a...@med-associates.comwrote in message

news:11*********************@e65g2000hsc.googlegro ups.com...
On May 9, 9:39 am, Dara P <D...@discussions.microsoft.comwrote:
Can some one suggest me as why StringBuilder class is better than
Strings?
Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.
Hello,

String class is immutable. Meaning any changes made to the reference
you have to a String object (such as concatenations), will be made on
a copy of the reference. Thus, as Nicholas mentioned, N strings
concatenations will allocate N strings for each concatenation.

StringBuilder uses double capacity allocation method, decreasing
allocation performance issues and assuring O(1) allocation complexity,
and it's better for multiple changes on a single string based objects
for the reasons mentioned above.

The following example will be much better using StringBuilder:

// This method will create almost 100000 String instances
String s = "MyName";
for( int i=0; i<100000; ++i)
s+=i.ToString();
// This will use only one instance and will allocate the space needed
for each append, efficiently. Much much better..
StringBuilder sb = new StringBuilder(s);
for( int i=0; i<100000; ++i)
sb.Append(i.ToString());

You can even make it better if you can predict the capacity prior to
making the changes. This will make sure allocation is made only when
necessary.

Hope it helps :)

May 9 '07 #5
Dara P wrote:
Can some one suggest me as why StringBuilder class is better than Strings?
For an example of where a StringBuilder is crucial for performance:

http://www.codeproject.com/script/co...=1#xx1519247xx

--
Göran Andersson
_____
http://www.guffa.com
May 9 '07 #6
On May 9, 8:35 am, Maverick <Moty...@gmail.comwrote:
On May 9, 6:02 pm, "Nicholas Paldino [.NET/C# MVP]"

<m...@spam.guard.caspershouse.comwrote:
It should be said that you should use StringBuilder to concatenate when
you don't exactly know how many strings you are going to concatenate. The
best example is when you have N number of strings and you need to append it
all together in a loop.
If you have a known number of strings, then using the concatenation
operator will work just fine, as it will compile to a call to the static
Concat method on the string class, which is very fast (it pre-allocates the
return buffer, and then does a memory copy of the strings into the
appropriate locations in the return buffer).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"Andy" <a...@med-associates.comwrote in message
news:11*********************@e65g2000hsc.googlegro ups.com...
On May 9, 9:39 am, Dara P <D...@discussions.microsoft.comwrote:
>Can some one suggest me as why StringBuilder class is better than
>Strings?
Its not better, it serves a different purpose. Use StringBuilder when
you have a lot of strings to concatinate, as the SB performs
concatination and other string manipulation operations faster than
using a standard string class.

Hello,

String class is immutable. Meaning any changes made to the reference
you have to a String object (such as concatenations), will be made on
a copy of the reference. Thus, as Nicholas mentioned, N strings
concatenations will allocate N strings for each concatenation.
Correction: N string concatenations will allocate N strings, ONE for
each concatenation, not N for each contatenation. The example that
followed gave the correct numbers.

May 10 '07 #7
In simple, when you use the Strings manipulation, Boxing and unboxing
happens. Where as when you use the Stringbuilder, as it clearly knows that
deals with Chars, so CLR will not undergo all the conversion and allocation
of extra memory bla.. bla..

HTH
--
Every thing is perfect, as long as you share!!!

Don''t forget to rate the post
"Dara P" wrote:
Can some one suggest me as why StringBuilder class is better than Strings?
--
Never say, Give up
May 10 '07 #8
Chakravarthy <ds******@india.comwrote:
In simple, when you use the Strings manipulation, Boxing and unboxing
happens.
No it doesn't. Consider this example:

string x = "hello";
x += "there";

Where do you believe the boxing is?
Where as when you use the Stringbuilder, as it clearly knows that
deals with Chars, so CLR will not undergo all the conversion and allocation
of extra memory bla.. bla..
String knows it's dealing with chars as well. Allocation is indeed the
issue, but not because of conversion or boxing. See
http://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
May 10 '07 #9
see
http://www.emoreau.com/Entries/Artic...c-Strings.aspx

--
HTH

Éric Moreau, MCSD, Visual Developer - Visual Basic MVP
Conseiller Principal / Senior Consultant
Moer inc. (http://www.emoreau.com)

"Dara P" <Da***@discussions.microsoft.comwrote in message
news:C8**********************************@microsof t.com...
Can some one suggest me as why StringBuilder class is better than Strings?
--
Never say, Give up

May 10 '07 #10
I've had to point my coworkers to Jon's StringBuilder page in the last five
jobs I've had.

///ark
May 10 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

73 posts views Thread by Rigga | last post: by
8 posts views Thread by Henning M | last post: by
12 posts views Thread by Richard Lewis Haggard | last post: by
26 posts views Thread by Hardy Wang | last post: by
5 posts views Thread by pantagruel | last post: by
13 posts views Thread by Tony Johansson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.