When exactly should a StringBuilder be used?

gicio

Regular
Joined
Aug 26, 2002
Messages
90
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
 
from what I understand is this, and someone please correct me if I'm wrong.

A lot of people (including myself) do things such as:
C#:
Console.WriteLine("The value of X = " + x.ToString() + "!")

Not a big problem there, but a more advanced application may have:
C#:
string ret;
for(int x=0;x<variable.Length;x++)
{
     ret += ";next value=" + variable + " and is " + boolVar.ToString();
}
Over a larger iteration this is much more work for the processor than having:
C#:
string ret;
for(int x=0;x<variable.Length;x++)
{
     ret.Append(";next value=" + variable + " and is " + boolVar.ToString();

}

now I heard that second hand, but it's something to think about if it's true.
 
i posted you a reply on codeguru :) , basically saying this ...
the easiest way to explain is , if you ever used vb6 , there might have been times when you needed to dim a string like this ....
Dim strString As String * 256
to make it have a buffer at the start, well the StringBuilder does basically that. here's 2 examples of using the API " GetClassName " , one using the StringBuilder , the other using just normal strings, try them and see the difference
using the StringBuilder .....
C#:
[DllImport("user32.dll", EntryPoint="GetClassNameA")]
static extern int GetClassName (int hwnd, System.Text.StringBuilder lpClassName, int nMaxCount);

private void button1_Click(object sender, System.EventArgs e)
{
    System.Text.StringBuilder str=new System.Text.StringBuilder(256); // buffer of 256 chars.
    GetClassName(base.Handle.ToInt32(),str,str.Capacity);
    MessageBox.Show(str.ToString());
}
using just a string ....
C#:
[DllImport("user32.dll", EntryPoint="GetClassNameA")]
static extern int GetClassName (int hwnd, string lpClassName, int nMaxCount);

private void button1_Click(object sender, System.EventArgs e)
{
    string str=string.Empty;
    GetClassName(base.Handle.ToInt32(),str,256);
    MessageBox.Show(str);
}
you will notice the string version returns an empty messagebox.
 
Additionally:

Every time you manipulate a STRING you are creating a NEW string editing a buffer and returning that value into the newly created string.

Stringbuilder keeps the same instace of the STRING and can edit its value.

If you are doing many edits to a string variable then StringBuilder is for you.

Psudo-codeish EXAMPLE:

PLACE TO USE STRING:
String X = "Hello" + name;

PLACE TO USE STRINGBUILDER:
StringBuilder X;
X = somevalue;
for(i=0; i < 10000; i++)
{
more = somefunction(i);
X += more;
}
 
Abbacabba - So then my post was actually pretty acurate... wonders will never cease!
 
I typically use it when I want to build a long, long string that will eventually ends up in a file.

Instead of writing to the file line by line, I create a stringBuilder, stuff it with my lines, and then write once.
 
Stringbuilders only have any speed usage when concatenating many thousands of strings. If you anywhere near 1000 id say use a stringbuilder, otherwise just use the normal & keyword. (making a new stringbuilder class takes more time then concat with a few &s)

For me the main use is building up data (for files) before throwing them out to disk, or over the network/internet. Often used in a for or loop statement they speed up your processing no end, if you work with data concat like I do, its worth moving over to .net just to use the stringbuilder.

e.g

Code:
        Dim strBuilder As StringBuilder = New StringBuilder()

        For intIndex = 0 To UBound(gMask)
             strBuilder.Append(gMask(intIndex))
        Next
   
        'Go save to disk (using strBuilder.ToString)
 
Back
Top