Strings are integral to most app dev projects, so it is imperative that they don't impair performance. The .NET StringBuilder class streamlines string values processing. It's hard to find an application that doesn’t deal with text in some way. As a result, developers are faced with a variety of string-related tasks every day. The .NET Framework includes many classes and methods for dealing with strings. One of these classes, StringBuilder, provides an approach to working with strings that is less resource intensive than its standard String class counterpart. Let's look first at why using String can lead to inefficient code. Then, we'll see how to use StringBuilder to save memory and processing time.
Placing strings in memory
When you declare and place a value in a .NET String data type, that value is stored in memory. The amount of memory utilised is determined by the actual text. The following code declares and populates a sample string:
Dim sample As String = "zeebyte.blogspot.com"
The same declaration in C# is:
C#:
string sample = "zeebyte.blogspot.com";
In both statements, a memory location is assigned with a length of 20. In fact, a new string (in memory) is created every time a String value is created. This is an important point, which we'll revisit when we look at the StringBuilder class. Let's examine a few more aspects of the String data type before we move on.
Although creating a static text value is routine, creating new values using multiple string data elements is a common task as well. You can use the plus sign (+) to piece together one or more text elements. The following sample illustrates concatenation in VB.NET first followed by C#.
VB.Net:
Dim sample1 As String = "zeebyte.blogspot.com"
Dim sample2 As String = "Tech Blog"
Dim sample3 As String
sample3 = sample1 + " " + sample2
C#:
string sample1 = "zeebyte.blogspot.com";
string sample2 = "Tech Blog";
string sample3;
sample3 = sample1 + " " + sample2;
Both code snippets result in the string "zeebyte.blogspot.com Tech Blog" and, in the process, four strings are created in memory. The sample1 string is created, followed by sample2 and then sample3. The last line creates another string in memory containing the results of the concatenation.
You may be thinking that this is a pointless concern—and you might be right, considering the miniscule amount of memory used by the sample code. But suppose you're creating a rather lengthy SQL statement using values from various sources. At this point, the amount of memory required (or wasted) and the processing time used for the creation of the numerous objects may become a bottleneck in your application. With this type of scenario in mind, Microsoft included the StringBuilder class in .NET.
StringBuilder class
The StringBuilder class allocates memory for a string according to characters. Additional memory is not always required using this approach. It contains various methods and properties, but we'll start by looking at basic usage. The StringBuilder class is located in the System.Text namespace, so you must reference this namespace to take advantage of it. The following code creates StringBuilder objects with our sample text.
VB.Net:
Imports System.Text
Dim sb As New StringBuilder("zeebyte.blogspot.com")
System.Console.Writeline(sb.ToString())
C#:
Using system.text;
StringBuilder sb = new StringBuilder("zeebyte.blogspot.com");
Console.WriteLine(sb.ToString());
The code results in the output of the "zeebyte.blogspot.com" string to the standard output or command line. It uses the ToString method to retrieve a String object with the text for presentation. The StringBuilder class provides other methods in addition to ToString. For example, the Append method allows multiple values to be concatenated like so:
VB.Net:
Dim sb As New StringBuilder("zeebyte.blogspot.com")
sb.Append(" Tech Blog")
C#:
StringBuilder sb = new StringBuilder("zeebyte.blogspot.com");
sb.Append(" Tech Blog");
You may be wondering if the StringBuilder code is more efficient than the code using String data types. The answer is no—at least not yet. As written here, the StringBuilder sample is not more efficient, because the system must reallocate memory to accommodate the new text appending to the end of the initial text. The initial length of the StringBuilder is set according to the value used to populate it. To make the code more efficient, you can use the Capacity property to allocate enough memory to handle the final value without memory reallocation and slowing down the system. The Capacity property sets the number of characters allocated for the string value. The following code reworks the previous code to set aside enough memory from the start:
VB.Net:
Dim sb As New StringBuilder()
sb.Capacity = 20
sb.Append("zeebyte.blogspot.com")
sb.Append(" Tech Blog")
C#:
StringBuilder sb = new StringBuilder();
sb.Capacity = 20;
sb.Append("zeebyte.blogspot.com");
sb.Append(" Tech Blog");
This code results in the allocation of plenty of memory from the beginning, so no memory reallocation is necessary (unless the capacity is surpassed). In addition to using the Capacity property to allocate memory for a specific number of characters, you can use the MaxCapacity property to establish the maximum size (ceiling) of the object.
One property often confused with Capacity is Length. The Length property returns the current size of the string value, and it may also be set to truncate or expand the size of the string. Setting the length to less than the current value truncates the string to the specified new length. The following code chops the value down to zeebyte.blogspot.com from the initial value:
VB.Net:
Dim sb As New StringBuilder("zeebyte.blogspot.com Tech Blog")
sb.Length = 10
System.Console.Writeln(sb.ToString())
C#:
StringBuilder sb = new StringBuilder("zeebyte.blogspot.com Tech Blog");
sb.length = 10;
Console.WriteLine(sb.ToString());
Establishing the capacity of the StringBuilder object before using it in your code ensures that the system will not have to continuously reallocate memory to accommodate a growing value (size). But even if reallocation is encountered, the StringBuilder approach is still more efficient than using base string objects where new objects must be repeatedly created. For this reason, you should use the StringBuilder object in situations where string values must be created on the fly. If you're creating a rather long string, you can take advantage of the With statement (VB.NET only) to further optimize the code:
VB.Net:
Dim sb As New StringBuilder()
With sb
.Capacity = 150
.Append("zeebyte.blogspot.com")
.Append(" ")
.Append("is")
.Append(" ")
.Append("the ")
.Append("Tech ")
.Append("Blog ")
End With
Manipulating the text
Once text has been placed in the StringBuilder object, you can use additional methods:
- The Chars method allows you to extract or replace individual characters.
- The Replace method allows you to replace all occurrences of a string with another string.
- The Insert method allows you to insert a value at a specified location.
A common task
Working with string values is a common task for all developers. Thankfully, the chore of building string values on the fly has been eased with the introduction of the StringBuilder class. This class provides the methods to easily manipulate text, and it is easy on system resources so program execution does not suffer.