| | |
C# : When exactly should a StringBuilder be used?
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2003
Posts: 23
Reputation:
Solved Threads: 0
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
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
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.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
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.
•
•
Join Date: Jun 2004
Posts: 32
Reputation:
Solved Threads: 1
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
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.
Why would you store all of that in a string? LOL
That's why we have things like DataSets...
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.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
•
•
Join Date: Jun 2004
Posts: 32
Reputation:
Solved Threads: 1
•
•
•
•
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?
•
•
Join Date: Jan 2005
Posts: 2
Reputation:
Solved Threads: 1
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:
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)
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();
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)
•
•
Join Date: Feb 2005
Posts: 1
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
Other Threads in the C# Forum
- Previous Thread: Opensource IDE
- Next Thread: Rookie Question
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox control conversion csharp custom customactiondata database datagrid datagridview dataset date/time datetime datetimepicker degrees development dll draganddrop drawing encryption enum event excel file filename files form format forms function gdi+ gis gtk hash httpwebrequest image index input install java label list listbox mandelbrot math mouseclick mysql operator outlook2003 path photoshop picturebox pixelinversion pixelminversion post programming radians regex remote remoting richtextbox server sleep socket sql statistics stream string table tables text textbox thread time timer tutorial update usercontrol usercontrols validation visualstudio webbrowser webcam wia windows winforms wpf xml







