C# : When exactly should a StringBuilder be used?

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2003
Posts: 23
Reputation: gicio is an unknown quantity at this point 
Solved Threads: 0
gicio gicio is offline Offline
Newbie Poster

C# : When exactly should a StringBuilder be used?

 
0
  #1
Nov 9th, 2003
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 StringBuilder();
stbTest.Append(strTest1). Append(“is a ”). Append(stbTest);


can someone provide some sample codes when to use a StringBuilder?

And can someone tell me his experience about the performance of a StringBuilder?


Regards,


gicio
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: C# : When exactly should a StringBuilder be used?

 
0
  #2
Nov 16th, 2003
StringBuilder should be used when putting together large, complex strings. Do not use it for working with small strings - StringBuilder has a lot of overhead and isn't worth the performance loss.
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,047
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 129
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: C# : When exactly should a StringBuilder be used?

 
0
  #3
Nov 16th, 2003
It seems to me as if gicio might indeed be using it for large strings, and is just providing these small "test" strings as an example to learn how to use it.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 32
Reputation: jackster is an unknown quantity at this point 
Solved Threads: 1
jackster jackster is offline Offline
Light Poster

Re: C# : When exactly should a StringBuilder be used?

 
0
  #4
Jun 18th, 2004
From my experience, if you are manipulating very large strings, such as imported from a 2MB text file you have to use StringBuilder.

To give you an example, to import a csv file of 30,000 records, when i used a string to store the data took over 2 mins to process (and used a lot of computer resources.

with StringBuilder, it tool just a few seconds to process.

jack
www.ansariltd.com

Originally Posted by cscgal
It seems to me as if gicio might indeed be using it for large strings, and is just providing these small "test" strings as an example to learn how to use it.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 898
Reputation: Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light Tekmaven is a glorious beacon of light 
Solved Threads: 28
Moderator
Tekmaven's Avatar
Tekmaven Tekmaven is offline Offline
The C# Man, Myth, Legend

Re: C# : When exactly should a StringBuilder be used?

 
0
  #5
Jun 18th, 2004
Why would you store all of that in a string? LOL

That's why we have things like DataSets...
-Ryan Hoffman

.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 32
Reputation: jackster is an unknown quantity at this point 
Solved Threads: 1
jackster jackster is offline Offline
Light Poster

Re: C# : When exactly should a StringBuilder be used?

 
0
  #6
Jun 22nd, 2004
Originally Posted by Tekmaven™
Why would you store all of that in a string? LOL

That's why we have things like DataSets...

Can you convert directly from a cvs file to a datatable?

I imported the cvs, into a string builder, then from that I manually converted it to a datatable by splitting it at the commans and end of lines.

is there an easier way?
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 22
Reputation: Decency is an unknown quantity at this point 
Solved Threads: 1
Decency Decency is offline Offline
Newbie Poster

Re: C# : When exactly should a StringBuilder be used?

 
0
  #7
Sep 1st, 2004
well from my experience you better not using the StringBuilder its full of over head, now if you are really dealing with very large strings you better use data sets or design a small data structure that accepts Object and proccess your data with.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 32
Reputation: jackster is an unknown quantity at this point 
Solved Threads: 1
jackster jackster is offline Offline
Light Poster

Re: C# : When exactly should a StringBuilder be used?

 
0
  #8
Sep 8th, 2004
how could u turna string straight to a dataaset
www.ansariltd.com
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 2
Reputation: SPYRO is an unknown quantity at this point 
Solved Threads: 1
SPYRO SPYRO is offline Offline
Newbie Poster

Re: C# : When exactly should a StringBuilder be used?

 
0
  #9
Jan 14th, 2005
It has nothing to do with the text size.
The difference is that the string value is immutable.
Methods that appear to modify a string actually return a new string containing the modification.

Here is a simple example:

string s1 = "World";
string s2 = s1.Insert(0, "Hello ");
label1.Text = object.ReferenceEquals(s1, s2).ToString();

StringBuilder sb1 = new StringBuilder("World");
StringBuilder sb2 = sb1.Insert(0, "Hello ");
label2.Text = object.ReferenceEquals(sb1, sb2).ToString();
Result:
label1.Text is “false�
label2.Text is “true�

s1 and s2 reference different objects, while sb1 and sb2 reference the same object.

Conclusion:
Use a StringBuilder if you have significant modifications of the text (to build a string)
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1
Reputation: markjuggles is an unknown quantity at this point 
Solved Threads: 0
markjuggles markjuggles is offline Offline
Newbie Poster

Re: C# : When exactly should a StringBuilder be used?

 
0
  #10
Feb 17th, 2005
I agree with SPYRO.

This is ultimately an issue of efficiency. If a program is constantly making minor string modifications without a StringBuilder, there will be a lot of unused object references which get garbage collected (eventually).

If you are working on a large number of items with several string manipulations each, the computer's performance might suffer. To get around this, manipulate the strings in a StringBuilder. There will be far fewer objects created.

Mark
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC