.NTOD StringBuilder()

Namespace : System.Text

Called the StringBuffer in Java, this type is a great way to dynamically build up a string without the need to use the + operator which concatenates strings together.  Breaking this down, a String is the in memory name for some text, upto 2,147,483,647 characters to be precise.  Adding strings together with the + operator is perfectly do-able in .NET but is not advised as its bad on performance.  Because strings are immutable (meaning they cannot be changed once created), a new string object has to be created for each concatenation that occurs to represent the resulting string.

String myfirstString = “my name is”;  //This creates a string object on the heap and points to it from the stack

String mySecondString = myfirstString + ” Scott”; //A new string object is created

One of the most common uses of string concatenation for me is the dynamic building of a SQL string that uses both literal text (the SQL query) and references to variables being used in the query.  The best way to build such a string is to use the StringBuilder class.  Ensure you are ‘using’ the System.Text namespace in your project.

The reason the string builder class is so useful is because it dynamically creates and destroys a char array on the fly as you append string values to the object. The object has a property called .Capacity which represents the max size of the objects current char array and by default this is 16 characters.  As you create your StringBuilder object and if you have a rough idea of how long your string is going to be,  you can set this property using the constructor.  When you append additional string values onto the StringBuilder object, internally the object checks what capacity is currently set, if the appended string does not exceed the current capacity of the array, then the current array is updated.  If the appended string does exceed the current array capacity, then the StringBuilder object creates a new array, with a higher capacity (well double the current length), copies your current char array into the new one and then destroys the old.  As with standard arrays, you can use the StringBuilders .Length property to check the current char array length.

String tableFQN = “Database.dbo.Customers”;

StringBuilder sb = new StringBuilder(“SELECT * FROM”); //Creates sb object with 16 character array

sb.Append(tableFQN); //Exceeds the default 16 chars and so a new 32 character array is created and the old destroyed

return sb.ToString()//returns SELECT * FROM Database.dbo.Customers as a System.String object

When you are ready to use the value of the object, you must then call the .ToString() method to convert the char array into a string object.  Just remember that strings are immutable where as the StringBuilder is mutable (can be changed).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s