Tuesday, May 1, 2012

String Builder Class

Author: Konjerla Suresh

In this Article I want to explain about the string builder class. How it useful when compared with string class

Since C# strings are immutable, an existing string cannot be modified. So, if one tries to change a string either with the concatenation operator (+) or with the Insert, Pad Left, Pad Right, Replace, or Substring methods, an entirely new string is created—leaving the original string intact. 

String1 s1 = “Suresh”;
String2 s2= “Kumar”;
String3 (new String) s3 = s1 + s2;
S3 -> Suresh Kumar.

Therefore, operations which would alter strings—instead—cause additional memory to be allocated. Memory is a scarce resource. And, memory allocations are expensive in terms of memory and performance. Consequently, sometimes String class usage should be avoided. 

The StringBuilder class is designed for situations when one needs to work with a single string and make an arbitrary number of iterative changes to it. Many StringBuilder class methods are the same as those of the String class. However, the string content of a StringBuilder class can be changed without the necessity of allocating additional memory. Thus, operations on the StringBuilder class will be much faster than operations on the String class in certain situation  

StringBuilder sb1 = “Suresh”;
StringBuilder sb2 = “Kumar”;
sb1 = sb1 + sb2;
sb1 -> Suresh Kumar

The String object is immutable while StringBuilder object is mutable. Both String and StringBuilder are reference type. But String acts like a value type.

StringBuilder is located in the System.Text namespace.

Performance Considerations:

A String object concatenation operation always creates a new object from the existing string and the new data. A StringBuilder object maintains a buffer to accommodate the concatenation of new data. New data is appended to the buffer if room is available; otherwise, a new, larger buffer is allocated, data from the original buffer is copied to the new buffer, and the new data is appended to the new buffer.

The performance of a concatenation operation for a String or StringBuilder object depends on the frequency of memory allocations. A String concatenation operation always allocates memory, whereas a StringBuilder concatenation operation allocates memory only if the StringBuilder object buffer is too small to accommodate the new data. Use the String class if you are concatenating a fixed number of String objects. In that case, the compiler may even combine individual concatenation operations into a single operation. Use a StringBuilder object if you are concatenating an arbitrary number of strings; for example, if you're using a loop to concatenate a random number of strings of user input


Gets or sets the maximum number of characters that can be contained in the memory allocated by the current instance.
Gets or sets the character at the specified character position in this instance.
Gets or sets the length of the current StringBuilder object.
Gets the maximum capacity of this instance.


Appends information to the end of the current StringBuilder.
Replaces a format specifies passed in a string with formatted text.
Inserts a string or object into the specified index of the current StringBuilder.
Removes a specified number of characters from the current StringBuilder.
Replaces a specified character at a specified index.
Converts the value of this instance to a String. (Overrides Object.ToString.)
Removes all characters from the current StringBuilder instance.

 The following program will explaing the string builder class 

using System;
using System. Text;
namespace StringBuilder_example
    class Program
        static void Main(string[] args)
            StringBuilder sbstr = new StringBuilder("We are the world");

            sbstr.Append (" we are the people");
            float currency=3400.50f;
            sbstr.AppendFormat(" Our individual salary : {0:C}. ", currency);
            sbstr.Insert(11, "people of ");
            Console.WriteLine (sbstr);
            sbstr.Remove (11, 10);
            Console.WriteLine (sbstr);
            sbstr.Replace ("world", "Indian");
            Console.WriteLine (sbstr);
            Console.ReadKey ();
Output :


No comments:

Post a Comment