1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Strings with StringBuilder in .NET

Discussion in 'ASP.NET' started by coderzone, Apr 25, 2007.

  1. coderzone

    coderzone Super Moderator

    Jul 25, 2004
    Likes Received:
    Trophy Points:
    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 = "go4expert.com"

    The same declaration in C# is:
    string sample = "go4expert.com";
    In both statements, a memory location is assigned with a length of 11. 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#.

    Dim sample1 As String = "go4expert.com"
    Dim sample2 As String = "Rules"
    Dim sample3 As String
    sample3 = sample1 + " " + sample2
    string sample1 = "go4expert.com";
    string sample2 = "Rules";
    string sample3;
    sample3 = sample1 + " " + sample2;
    Both code snippets result in the string "go4expert.com Rules" 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.

    Imports System.Text
    Dim sb As New StringBuilder("go4expert.com")
    Using system.text;
    StringBuilder sb = new StringBuilder("go4expert.com");
    The code results in the output of the "go4expert.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:
    Dim sb As New StringBuilder("go4expert.com")
    sb.Append(" Rules!")
    StringBuilder sb = new StringBuilder("go4expert.com");
    sb.Append(" Rules!");
    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:
    Dim sb As New StringBuilder()
    sb.Capacity = 18
    sb.Append(" Rules!")
    StringBuilder sb = new StringBuilder();
    sb.Capacity = 18;
    sb.Append(" Rules!");
    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 go4expert.com from the initial value:
    Dim sb As New StringBuilder("go4expert.com Rules!")
    sb.Length = 11
    StringBuilder sb = new StringBuilder("go4expert.com Rules!");
    sb.length = 11;
    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 optimise the code:

    Dim sb As New StringBuilder()
    With sb
    .Capacity = 200
    .Append(" ")
    .Append(" ")
    .Append("the ")
    .Append("programming ")
    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.

Share This Page