Part 26 String Builder

String Builder :
A String Builder is mutable, mean the contents of a string object value can be changed after the object is created.

1. String Builder is located in the System.Text namespace.

2. It will represent a sequence of characters or Array of characters and perform a same kind of operations string.

3. If any changes made to the string Builder is dynamic object like append an existing value, then it will not create another memory location simply It doesn't create a new object in the memory but dynamically expands memory.


Examplestring "Hello chandan" will consume a memory space on the heap. by changing the initial string "Hello chandan" to "Hello from csharp-tutorial-point" will create a new string object on the memory heap instead of modifying the initial string at the same memory address. The performance if the same string changes multiple times by replacing, appending, removing or inserting new strings in the initial string.


To solve this problem,introduced StringBuilder. StringBuilder is a dynamic object that allows you to expand the number of characters in the string. It doesn't create a new object in the memory but dynamically expands memory to accommodate the modified string.




StringBuilder.Append
Appends the data to the end of the current StringBuilder.
StringBuilder.AppendFormat
Replaces a format specifier passed in a string with formatted text.
StringBuilder.Insert
Inserts a string into the specified index of the current StringBuilder.
StringBuilder.Remove
Removes a specified number of characters from the current StringBuilder.
StringBuilder.Replace
Replaces a specified character at a specified index.

Append() : Append method of StringBuilder to the add or append a string. 

            StringBuilder sb = new StringBuilder("Chandan");
            sb.Append(", Pandey");

            Console.WriteLine(sb);
output:
chandan, Pandey


AppendLine() : method appends the string with a newline at the end.


            StringBuilder sb = new StringBuilder("Chandan");
            sb.Append(", Pandey");
            sb.AppendLine(", Software Developer");
            Console.WriteLine(sb);
output:
chandan, Pandey
Software Developer

Insert(): Inserts the string at specified index in StringBuilder.

StringBuilder sb = new StringBuilder("Welcome to C#",50);
sb.Insert(11," world");
Console.WriteLine(sb);
Output
welcome to c# world
Remove(): Removes the string at specified index with specified length.
StringBuilder sb2 = new StringBuilder("Welcome to CSharp-tutorial-point");   
             sb2.Remove(8, 3);
            Console.WriteLine(sb2);
Output: Welcome CSharp-tutorial-point
Replace() ; Occurance of a specified string with a specified replacement string. StringBuilder sb3 = new StringBuilder("Welcome to Csharp World"); sb3.Replace("Csharp", "C#"); Console.WriteLine(sb3); Output: Welcome to C# World


Note : StringBuilder perform faster than string when concatenating multiple strings. Use StringBuilder

Example :

class Program

    {       
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Chandan");
            //StringBuilder Append data
            sb.Append(", Pandey");
            Console.WriteLine(sb);
            StringBuilder sb1 = new StringBuilder("Welcome");
           //Insert data
            sb1.Insert(7, "to Chandan Pandey");
            Console.WriteLine("Insert String: " + sb1);
            StringBuilder sb2 = new StringBuilder("Welcome to CSharp-tutorial-point");
            sb2.Remove(8, 3);
            Console.WriteLine(sb2);
            StringBuilder sb3 = new StringBuilder("Welcome to Csharp World");
            sb3.Replace("Csharp", "C#");
            Console.WriteLine(sb3);
            Console.ReadKey();
        }
    }

Output


Chandan, Pandey
Insert String: Welcometo Chandan Pandey
Welcome CSharp-tutorial-point
Welcome to C# World