Im 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?



14 Years
Discussion Span
Last Post by jujubee

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.


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.


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.


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...

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?


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.


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();

label1.Text is “false
label2.Text is “true

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

Use a StringBuilder if you have significant modifications of the text (to build a string)


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.



how could u turna string straight to a dataaset

I recently wrote a program that did exactly that. It imported a .csv file into a dataset then exported all the data to a SQL Server 2000 DB. I would give you the source, except there is a lot happening in there that doesn't apply to this so it would be confusing.

Basically what I did was I read one line of text at a time, then I divided that text by the commas. Then I used each of those "cells" as the data for a datatable that I just looped over at the same time I was looping over the CSV file.


SPYRO is right. Any time that you are processing in a loop to build a string, or as some of you have stated, manipulating a large string, StringBuilder is the way.

The overhead of StringBuilder is nothing when compared to the overhead created by a loop that run through 1000 iterations, and creates a 1000 string objects (or more).

StringBuilder = 1 object for entire operation.

Seth Webster


What spyro said -
the number of times you need to add/modify the string is the cause of overhead -

here is its basic usage:
//add reference
using System.Text

//create string builder
StringBuilder sb = new StringBuilder();
//add items to it
sb.Append("some text");
sb.Append(" more text");
//output contents, in this case to the lblOutput label

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.