.NET String vs StringBuilder

by Abe Miessler 8. December 2009 03:44

A common mistake made by new programmers is to not utilize the StringBuilder class in the .Net framework.  To begin understanding when we should use StringBuilder let's first look at some of the inner workings of strings.

The most important thing to know about strings is that they are immutable.  That is, once you have created a string you can never change it.  It will never get longer, shorter or have any of its characters changed.  This is a difficult concept for many people to understand.  Lets look at the code below:

string Name = "Abe";

Name = Name + "miester";

While it might appear that you are actually just adding "miester" to the end of "Abe" in order to update the existing string what is happening in the background is actually much different.  When you assign the new value to the Name variable two entirely new strings are created.  There are now actually three strings in memory: "Abe", "miester" and "Abemiester".  The strings "miester" and "Abe" are no longer needed and the garbage collector will reclaim their memory at the next collection.

Now that we have an idea of how strings work lets look at a situation that could be improved by the use of the StringBuilder class.  Lets look at the code below:

 

    string letsCount = "Let's count! ";

    for(int i=; i< 100; i++)

    {

         letsCount += i + ", ";

    }

    Console.WriteLine(letsCount);

 

The above code will create a string that counts from 0 to 100.  Unfortunately, since we are using the string class, a new string will be created on the heap every iteration.  To avoid this we can use the StringBuilder class.

The StringBuilder class is provided as a means to dynamically construct a string in an efficient manner.  A StringBuilder object represents a string as an arrary of Char structures, each Char can have it's value changed and the array can be shrunk.  If the string grows beyond the allocated array of characters, StringBuilder automatically creates a new, larger array, copies the contents over, and begins using the new array.  It should be noted that dynamically growing your StringBuilder contents hurts performance so you should do your best to set a good initial capacity.  When you are working with a String who's value changes frequently this is much more efficient.  Lets look at the code below:

    using System.Text;

    StringBuilder letsCount = new StringBuilder("Let's count! ", 150); //Initialize the string to "Let's count!" and set the initial length to 150

    for(int i=; i< 100; i++)

    {

         letsCount..AppendFormat("{0}, ",i);

    }

    Console.WriteLine(letsCount.ToString());

 

This method is much more efficient because we are not creating a new string on every iteration.  In situations where you are dynamically adding to a string you should try to use this method whenever possible.

 

Update:

After talking with some of the folks at StackOverflow.com I found out that the fastest way to do string appends is actually to use the String Join method.  While StringBuilder does offer an improvement, Join performs even better.  Take a look at this post for more details on why.

 

Tags: , , ,

ASP.NET | StringBuilder

Comments

12/8/2009 6:07:40 PM #

Dustin

Nice post, one of the better explanations on strings I have seen.

Dustin United States |

Powered by BlogEngine.NET 1.6.0.0