Mutable string in C#

In this article we will discuss about how to use the StringBuilder class in C#.
  • 14333

Mutable strings are those strings which can be modified. In general way when we modify an existing string in C# we think that we have applied the changes to the existing object, in fact when we done any kind of modification in string a new object is created which refer to that modified string and the old one become unreferenced and as a result the garbage collector then collect that unreferenced object. If there is situation occurs that we are continuously modifying the string then the no. of unreferenced object will increase then it will be the overhead for the garbage collector to free the unreferenced object. Thus, in this case rather than using string class we will use StringBuilder class. This class allows the modification in the same object. Thus in this case the overhead of the garbage collector will be reduced and the performance of the program will be increase.

Lets have a look on the following operation which are associated with the StringBuilder class.
 
Property or Method Description
Length Gets the no. of character that the StringBuilder  object  contains.
Capacity Gets the current capacity of the StringBuilder object.
Append() Append the string representation of the specified object at the end of this StringBuilder instance.
Insert() Append the string representation of the specified object at the specified index of this StringBuilder instance.
Replace(string,string) Replaces all the occurrence of the first supplied string with the second supplied string in this Stringbuilder object.
Remove(int st,int length) Remove all characters from the index position st of specified length in the current StringBuilder object.
Equals(StringBuilder) Checks the supplied stringbuilder object with this instance & returns true if both are identical:otherwise,it returns false.

using System.Text;

 

namespace ConsoleApplication8

{

    class Program

    {

        static void Main(string[] args)

        {

            StringBuilder sb = new StringBuilder("This book");

            string s = "is completed";

            Console.WriteLine("Length of the stringbuilder {0} is {1}",sb,sb.Length);

            Console.WriteLine("Capacity of the stringbuilder {0} is {1}",sb,sb.Capacity);

            Console.WriteLine("StringBuilder before appending is {0}", sb);

            Console.WriteLine("StringBuilder after appending {0} is {1}",s,sb.Append(s));

            Console.WriteLine("stringbuilder after inserting now is {0}",sb.Insert(11,"now"));

            Console.WriteLine("StringBuilder after removing 'is' is {0}",sb.Remove(8,3));

            Console.WriteLine("Stringbuilder replacing all 'o' with 'x' is {0}", sb.Replace('o', 'x'));

            Console.ReadLine();

        }

    }

}


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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.