We know that string objects are immutable. The term Immutable means once the object string is created then it can’t be modified. Now take an example to understand string builder suppose a string “Hello” is created so it will occupy its space on the memory heap. Now we want to change or modify it for some reason to “Hello Mind stick” then it will not modify the initial one it will create another object of “Hello Mind Stick” it will create new string object on the memory heap instead of modifying.

This is a drawback of string. String Builder solves all this type of issue. String Builder is mute. String Builder is a string that can be modified. It is a dynamic property it expand the number of character in string. 

Now we will take two diagram to understand string and string Builder.

String Builder

In the above diagram it is clear that string can’t change it make another heap.

String Builder

It is faster than string we need to concatenate.

Method of string builder.

1-Append()

2-AppendFormat()

3-Insert()

4-Remove()

5-Replace() 

Initialization of stringBuilder. It is same as the class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stringbuffer
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Hello Mindstick!!",50);
            Console.WriteLine(sb);
        }
    }
}
 

 

Append method is use to concatenate string.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stringbuffer
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Hello Mindstick!!",50);
            sb.Append("Civil Lines");          
            Console.WriteLine(sb);
        }
    }
}
 

AppendLine method is use to append string in the next line.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stringbuffer
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Hello Mindstick!!",50);
            sb.Append("Civil Lines");
            sb.AppendLine("Software Development");
            Console.WriteLine(sb);
        }
    }
}
 

Appendformat()

It is use to format in specific format.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stringbuffer
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Salary above!!");
            sb.AppendFormat("{0:C}",500);
            sb.AppendLine("Software Development");
            Console.WriteLine(sb);
        }
    }
}
 

 

Insert()

Insert method is use to insert string at specific position. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stringbuffer
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Civil line Allahabad!!");
            sb.Insert(11, "Takshandmarg ");
           
            Console.WriteLine(sb);
        }
    }
}
 

Remove()

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stringbuffer
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Civil line Allahabad!!");
            sb.Remove(6, 4);
           
            Console.WriteLine(sb);
        }
    }
}
 


Replace() 

Replaces all string with new string. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace stringbuffer
{
    class Program
    {
        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder("Civilline Allahabad!!");
            sb.Replace("Allahabad", "Mindstick");
           
            Console.WriteLine(sb);
        }
    }
}
  Modified On Mar-17-2018 04:21:24 AM

Leave Comment