Skip Navigation LinksHome > Categories > Code from a Category

Stringbuilder Class in C#.NET

User Name: codelecturer
Name: Mike Chauhan
Contact Me:
Home Page:
6 years of total IT experience including programming, application development, System Maintenance . 3 years of Experienced and expertise in .Net Framework VB, Visual Basic.Net, ASP.Net 2.0, 3.5 and AD... [More]
Viewed Times: 1088
Add Date: 10/12/2012
In this article we will learn about 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.

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.


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.


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


Append line is used to add one line to existing string builder variable data. You can concatenate blank line or with data.


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.

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.

Happy Programming!!!

Post a Comment

Name: (Optional)
Email: (Optional, you can get an email if somebody replys your comments)*
Email me if somebody respons my comment below:
Enter Text
as Below:
(case insensitive, if hard to read, click the "get a new one" button)
* Your email address will not be shared with any third parties for any reason.
** Maximum 1000 charactors.