943,896 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 54242
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 9th, 2003
0

C# : When exactly should a StringBuilder be used?

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
gicio is offline Offline
23 posts
since Oct 2003
Nov 16th, 2003
0

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

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.
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Nov 16th, 2003
0

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

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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Jun 18th, 2004
0

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

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

Quote 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.
Reputation Points: 10
Solved Threads: 1
Light Poster
jackster is offline Offline
32 posts
since Jun 2004
Jun 18th, 2004
0

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

Why would you store all of that in a string? LOL

That's why we have things like DataSets...
Moderator
Reputation Points: 322
Solved Threads: 28
The C# Man, Myth, Legend
Tekmaven is offline Offline
914 posts
since Feb 2002
Jun 22nd, 2004
0

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

Quote 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?
Reputation Points: 10
Solved Threads: 1
Light Poster
jackster is offline Offline
32 posts
since Jun 2004
Sep 1st, 2004
0

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

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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Decency is offline Offline
22 posts
since Aug 2004
Sep 8th, 2004
0

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

how could u turna string straight to a dataaset
Reputation Points: 10
Solved Threads: 1
Light Poster
jackster is offline Offline
32 posts
since Jun 2004
Jan 14th, 2005
0

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

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:

Quote ...
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)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
SPYRO is offline Offline
2 posts
since Jan 2005
Feb 17th, 2005
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
markjuggles is offline Offline
1 posts
since Feb 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Opensource IDE
Next Thread in C# Forum Timeline: Rookie Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC