StringBuilder class in C#.NET


As we know the String object is an immutable and you need to create a new object when you want to using the string which requires a new allocation of a memory space for that new object. For example if you want modify the string the new memory will be allocated for it. For every operation creating a memory space cost thing.

To overcome this situation and if we need to work efficiently with string operations we have a StringBuilder class in .NET. You can use this string builder when you want to modify a string without creating a new object. So we can say StringBuilder is a more efficient class in .NET than the string.
As string builder is a dynamic object it will allow the length of the string automatically. We can declare the StringBuilder length like as shown below.
You can create a new instance of the StringBuilder class like as below.


StringBuilder str = new StringBuilder();
str.Capacity = 60;
StringBuilder strLenght = new StringBuilder("This is sample string Builder",50);


We can increase the length by using Capacity property of string builder like as shown above. For example if we declare the length 60, but the actual assigned string length exceeds it the string length will be increased automatically and it allow to assign all the string at a time.
We are having several methods on sting builder to update it.

Replace() :

Replace is a one of the best features of StringBuilder, it allows to replace the string or character or special character in string builder. The example is like as shown below.

StringBuilder strLenght = new StringBuilder("This is sample string Builder!!!",50); strLenght.Replace('!','.');


Remove():

By using this method you can remove the specific number if characters from your current string. You have to provide the starting and ending indexes to remove.

StringBuilder strLenght = new StringBuilder("This is sample string Builder!!!",50);
strLenght.Remove(29,31);

Append():

Append is a one of powerful feature available in String Builder class and it is used to append the data to string builder.

StringBuilder str = new StringBuilder();
str.Capacity = 60;
StringBuilder strLenght = new StringBuilder("This is sample string Builder!!!",50);
strLenght.Append("You can use the string builder easily and efficiently");
Console.WriteLine(strLenght);


AppendLine():

Append line is used to add one line to existing string builder variable data. You can concatenate blank line or with data.
 StringBuilder str = new StringBuilder();
str.Capacity = 60;
StringBuilder strLenght = new StringBuilder("This is sample string Builder!!!",50);
strLenght.AppendLine();
strLenght.Append("You can use the string builder easily and efficiently");
strLenght.AppendLine();
Console.WriteLine(strLenght);



Insert():

It is a wonderful mechanism and it allows you to insert the string in specified the index position of existing sting. Here you need to provide the index number to insert.

StringBuilder strLenght = new StringBuilder("This is sample string Builder!!!",50);
strLenght.Insert(33,"You can use the string builder easily and efficiently");
strLenght.AppendLine();
Console.WriteLine(strLenght);



Difference between String and StringBuilder


1) String is a class in C#.
2) Name space is System.String.
3) Strings are immutable; if you try to modify the string it actually creates a new string.
4) Once the string object is created, that string length and content cannot be modified.
5) The most common operation in String is Concatenation.

1) String Builder also a class in C#.
2) Name space is System.StringBuilder.
3) String Builder is mutable one.
4) Even after object is created, it can be able to modify length and content.
5) The most common Operation in String Builder is Append.


Comments

Author: KUNDAN KUMAR SRIVASTAVA17 Feb 2012 Member Level: Gold   Points : 3

StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the object and should not be confused with the length of the string that the current StringBuilder holds. For example, you might create a new instance of the StringBuilder class with the string "Hello", which has a length of 5, and you might specify that the object has a maximum capacity of 25. When you modify the StringBuilder, it does not reallocate size for itself until the capacity is reached. When this occurs, the new space is allocated automatically and the capacity is doubled. You can specify the capacity of the StringBuilder class using one of the overloaded constructors. The following example specifies that the MyStringBuilder object can be expanded to a maximum of 25 spaces.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: