Friday, January 12, 2007

What's so bad about string concatenation?

A lot of developers use the + operator to concatenate multiple strings. This works fine so what is wrong with this approach?

First of all, strings are immutable objects. An immutable object is an object that can't be modified after it has been created. So when using the + operator, in case of adding the second string at the end of the first string, .NET creates a new string with the content of both strings. This can cause an object-allocation overhead and put pressure on the garbage collector.

There are 2 possible solutions to avoid this:

  • use the string.Format() method
  • use the StringBuilder class

I'll give an example of both methods. Since the String.Format method is normally used for formatting strings, as the name suggests, I will focus myself on the StringBuilder.

I. String.Format() method

When using this method to concatenate strings, you need to create one string with a number of "replacers" ({0}, {1}, {2}, ..., {n}). Each replacer will be replaced by the referenced argument ({0} will reference to the first argument, {1} will reference to the second argument). The number of replacers must be same as the number of arguments.

Here is an example:

string value1 = "Geert Verhoeven";
string value2 = "Belgium";

string result = string.Format("My name is {0} and I live in {1}.", value1, value2);

The string in result will be: My name is Geert Verhoeven and I live in Belgium.

Extra info: http://msdn2.microsoft.com/en-us/library/fht0f5be.aspx



II. StringBuilder method

The StringBuilder class can be found in the System.Text namespace so you will need to add a using statement.

First you need to create a StringBuilder object. The StringBuilder class has a method "Append" to append strings. When you added the last string to the StringBuilder, you can retrieve the entire string by using the ToString() method of the StringBuilder class.

StringBuilder sb = new StringBuilder();
sb.Append("This is the first part of the string");
sb.Append("This is the second part of the string");
sb.Append("This is the third part of the string");
sb.Append("This is the fourth part of the string");

string
entireString = sb.ToString();

How it works:
A StringBuilder object internally contains a character buffer. This buffer expands as you append characters. When creating a StringBuilder it has enough space for 16 characters. If you go beyond 16 characters, the class creates a new buffer which is double the size as the previous buffer.

Too much expanding can affect the performance of the StringBuilder class. To avoid this, you can specify the initial size of the buffer when creating a new StringBuilder instance.

StringBuilder sb = new StringBuilder(1024);

Extra info: http://msdn2.microsoft.com/en-us/library/system.text.stringbuilder.aspx

No comments: