SIGN UP MEMBER LOGIN:    
ARTICLE

Modification in string using Stringbuilder class in C#

Posted by Hirendra Sisodiya Articles | C# Language July 27, 2010
Modification in string using Stringbuilder class in C#
Reader Level:

Modification in string using Stringbuilder class in C#

String can be modified by using stringbuilder class. We can take an example for better understanding, suppose there are two string string11 and string2:

            StringBuilder String1 = new StringBuilder("Hello");

            StringBuilder String2 = new StringBuilder();

On above String1 is created with five characters 'Hello' and String is created as an empty string, we can modified these string dynamically such as we can append these string or add some string on it.so we can say that this class can be used when we want to modify a string without creating a new object. On here String1 and String2 can be also called by dynamic strings or mutable strings.



Methods:

Append

Appends information to the end of the current StringBuilder.

AppendFormat

Replaces a format specifier passed in a string with formatted text.

Insert

Inserts a string or object into the specified index of the current StringBuilder.

Remove

Removes a specified number of characters from the current StringBuilder.

Replace

Replaces a specified character at a specified index.


Example:


private
void Example_StringBuilder()

        {

            StringBuilder String1 = new StringBuilder("beautiful");

            StringBuilder String2 = new StringBuilder();

 

 

            String2 = String1.Append(" Picture");

            // value of String2="beautiful Picture"

 

            String2 = String1.Remove(2, 4);

            // value of String2="beful Picture"

 

            String2 = String1.Insert(2, "auti");

            // value of String2="beautiful Picture"

 

            String2 = String1.Replace("beautiful", "Wonderful");

            // value of String2="Wonderful Picture"

          }

 

Login to add your contents and source code to this article
share this article :
post comment
 

In your example, you use the Stringbuilder object as though it is immutable:

String2 = String1.Append(" Picture");

What you have done here, was to append " Picture" to String1, and then just changed the String2 reference to point to String1.

You don't need to have 2 instances of the StringBuilder. Rather just create one, and treat it like the mutable object it is...

Better Example:

StringBuilder String1 = new StringBuilder("beautiful");

String1.Append(" Picture");
//String1="beautiful Picture"
 
String1.Remove(2, 4);
//String1="beful Picture"
 
String1.Insert(2, "auti");
//String1="beautiful Picture"
 
String1.Replace("beautiful", "Wonderful");
//String1="Wonderful Picture"

Posted by Nico Gerber Jul 28, 2010

What is wrong!

Posted by Hirendra Sisodiya Jul 28, 2010

You are using the mutable StringBuilder objects as though they are immutable.

This is more accurate:

StringBuilder String1 = new StringBuilder("beautiful");
StringBuilder String2 = new StringBuilder();
//String1=beautiful
//String2=

String2 = String1.Append(" Picture");
//String1=beautiful Picture
//String2=beautiful Picture

String2 = String1.Remove(2, 4);
//String1=beful Picture
//String2=beful Picture

String2 = String1.Insert(2, "auti");
//String1=beautiful Picture
//String2=beautiful Picture

String2 = String1.Replace("beautiful", "Wonderful");
//String1=Wonderful Picture
//String2=Wonderful Picture

Posted by Nico Gerber Jul 28, 2010
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor