Working with StringBuilder Class in C#

In this article we will discuss about what is StringBuilder Class in C#.
  • 2532

The StringBuilder Class is used for mutable string .i.e. the string which can be changed. Creating a new object every time we change the string sounds reasonable if our program deals with strings having moderate length. But if the program extensively performs text processing, then this may affect the performance of the program as there may be a number of unreferenced objects on the heap waiting to be collected by the garbage collector. Hence .NET provided other another class named StringBuilder to cope with this problem.

Although StringBuilder does not provide as much functionality as the String class, it certainly provides methods for the most common operations. To be able to use StringBuilder Class we must import classes of System.Text namespace.

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

namespace absclass

{

    class Program

    {

        static void Main(string[] args)

        {

            StringBuilder sb = new StringBuilder("punet");

            sb.Insert(3, "e");

            Console.WriteLine("The new string is: "+sb);

            Console.ReadLine();

        }

         

    }

   

}

Output:

Image12.jpg

Further Readings
 
You may also want to read these related articles.

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

© 2020 DotNetHeaven. All rights reserved.