Introduction
Part 1
Part 2
Part 3
MSDN Reference
Well, you need to learn Part 1 of this series of articles to understand this better. In the previous articles we tried to change the content of strings using various methods like Replace(), ToUpper() etc. But if you recall, the value of a string cannot be modified once it is established. The methods like Replace() may seem to change the content of the string but actually, those methods just return a copy of the string and the original string remains the same.
Look at example:
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str1 = "Hello Abhimanyu";
Console.WriteLine(str1);
string str2 = str1.ToUpper();
Console.WriteLine(str2);
//Let's see original string variable
Console.WriteLine(str1);
Console.ReadKey();
}
}
}
/*
Output:-
Hello Abhimanyu
HELLO ABHIMANYU
Hello Abhimanyu
*/
Thus, whenever we want to modify a string, we should have a new string to store the modified version. That is, every time we have to work on a copy of the string but not the original. To avoid this inefficiency, C# provides a class called StringBuilder contained in the namespace System.Text. Any modification on an instance of StringBuilder will affect the underlying buffer itself.
Let's have an example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; //for StringBuilder
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StringBuilder str1 = new StringBuilder("Hello Abhimanyu");
Console.WriteLine(str1);
str1.Append(", how are you?");
Console.WriteLine(str1);
Console.ReadKey();
}
}
}
/*
Output:-
Hello Abhimanyu
Hello Abhimanyu, how are you?
*/
To modify the string content, we have the following methods:
(i) Append: Appends information to the end of the current StringBuilder.
(ii) AppendFormat: Replaces a format specifier passed in a string with formatted text. Read and modify method.
(iii) Insert: Inserts a string or object into the specified index of the current StringBuilder.
(iv) Remove: Removes a specified number of characters from the current StringBuilder.
(v) Replace: Replaces a specified character at a specified index.
Let's have an example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; //for StringBuilder
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Using StringBuilder.Append()
StringBuilder str1 = new StringBuilder("Hello Abhimanyu");
Console.WriteLine(str1); //output: Hello Abhimanyu
str1.Append(", how are you?");
Console.WriteLine(str1); //output: Hello Abhimanyu, how are you?
//Using StringBuilder.AppendFormat()
string sen1 = "Hello";
string sen2 = "Abhimanyu";
string sen3 = ", how are you?";
StringBuilder str2 = new StringBuilder();
str2.AppendFormat("{0}", sen1); //output: Hello
Console.WriteLine(str2);
str2.AppendFormat(" {0}", sen2); //output: Hello Abhimanyu
Console.WriteLine(str2);
str2.AppendFormat(" {0}", sen3); //output: Hello Abhimanyu, how are you
Console.WriteLine(str2);
//Using StringBuilder.Insert()
StringBuilder str3 = new StringBuilder("Hello ");
str3.Insert(6, "Abhimanyu");
Console.WriteLine(str3); //output: Hello Abhimanyu
//Using StringBuilder.Remove()
StringBuilder str4 = new StringBuilder("Hello .");
Console.WriteLine(str4); //output: Hello .
str4.Remove(6, 1);
Console.WriteLine(str4); //output: Hello
//Using StringBuilder.Replace()
StringBuilder str5 = new StringBuilder("Hello Abhi");
Console.WriteLine(str5); // output: Hello Abhi
str5.Replace("Abhi", "Abhimanyu");
Console.WriteLine(str5); // output: Hello Abhimanyu
Console.ReadKey();
}
}
}
Thank you for reading. Keep Commenting.
HAVE A HAPPY CODING!!