C# StringBuilder is similar to Java StringBuilder. A String object is immutable, i.e. a String cannot be changed once created. Every time when you use any of the methods of the System.String class, then you create a new string object in memory. For example, a string “TechforTech” occupies memory in the heap, now, by changing the initial string “TechforTech” to “TAS” will create a new string object on the memory heap instead of modifying the initial string at the same memory location.
In situations where you need to perform repeated modifications to a string, we need StringBuilder class. To avoid string replacing, appending, removing or inserting new strings in the initial string C# introduce StringBuilder concept. StringBuilder is a dynamic object. It doesn’t create a new object in the memory but dynamically expands the needed memory to accommodate the modified or new string.
Declaration and Initialization of StringBuilder
StringBuilder can be declared and initialized the same way as class,
StringBuilder s = new StringBuilder(); or StringBuilder s = new StringBuilder("TechforTech");
“s” is the object of StringBuilder class. Also, we can pass a string value(here “TechforTech”) as an argument to the constructor of StringBuilder.
Defining the capacity of StringBuilder
Although the StringBuilder is a dynamic object that allows you to expand the number of characters in the string that it encapsulates, you can specify a value for the maximum number of characters that it can hold. This value is called the capacity of the StringBuilder object.
StringBuilder s = new StringBuilder(20); or StringBuilder s = new StringBuilder("TechforTech", 20);
Here,
- In the 1st statement we pass an integer value as an argument to the constructor. This is the maximum capacity of character that can hold a string.
- In the 2nd statement we pass string value with an integer value (that is the maximum capacity of character a string can hold) as an argument to the constructor.
Important Methods of StringBuilder Class:
- Append(string value)
- AppendFormat()
- Insert(int index, string value)
- Remove(int start, int length)
- Replace(old_val, new|_val)
StringBuilder.Append(string value) Method
The Append method can be used to add or append a string value of an object to the end of a string represented by the current StringBuilder object. AppendLine() method also come under this method. This method append the string with a newline at the end.
Example:
// C# program to demonstrate the // StringBuilder.Append(value) and // StringBuilder.AppendLine(value) method using System; using System.Text; class TAS { // Main Method public static void Main() { // "20" is capacity StringBuilder s = new StringBuilder("HELLO ", 20); s.Append("TAS"); // after printing "TECH" // a new line append s.AppendLine("TECH"); s.Append("TechforTech"); Console.WriteLine(s); } }
Output:
HELLO TASTECH TechforTech
StringBuilder.AppendFormat()
This method uses to format the input string into the specified format and then append it. This method also appends text to the end of the StringBuilder object.
// C# program to demonstrate the // StringBuilder.AppendFormat() method using System; using System.Text; class TAS { // Main Method public static void Main() { StringBuilder s = new StringBuilder("Your total amount is "); // using the method s.AppendFormat("{0:C} ", 50); Console.WriteLine(s); } }
Output:
Your total amount is ¤50.00
StringBuilder.Insert(int index, string value) method
This method inserts the string at specified index in StringBuilder object.
Example:
// C# program to demonstrate the // StringBuilder.Insert(int index, // string value) method using System; using System.Text; class TAS { // Main Method public static void Main() { // "20" is capacity StringBuilder s = new StringBuilder("HELLO ", 20); // "TECH" insert after 6th index s.Insert(6, "TECH"); Console.WriteLine(s); } }
Output:
HELLO TECH
StringBuilder.Remove(int start, int length) Method
This method removes the specified number of characters from the current StringBuilder object. The removing process beginning at a specified index and extends up to another specified index.
Example:
// C# program to demonstrate the // StringBuilder.Remove(int index, // int length) method using System; using System.Text; class TAS { // Main Method public static void Main() { // "20" is capacity StringBuilder s = new StringBuilder("TechforTech", 20); // remove starts from index 5 // and remove happes 3 index // after index 5 s.Remove(5, 3); Console.WriteLine(s); } }
Output:
TechTech
StringBuilder.Replace(old_val, new_val) Method
This method is used to replace characters within the StringBuilder object with another specified character.
Example:
// C# program to demonstrate the // StringBuilder.Replace(string old_val, // string new_val) method using System; using System.Text; class TAS { // Main Method public static void Main() { // "20" is capacity StringBuilder s = new StringBuilder("TAS Tech ", 20); // Replace "TAS" with "Tech For" s.Replace("TAS", "Tech For"); Console.WriteLine(s); } }
Output:
Tech For Tech
Recent Comments