StringBuilder in C#
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 “GeeksForGeeks” occupies memory in the heap, now, by changing the initial string “GeeksForGeeks” to “GFG” 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("GeeksforGeeks");
“s” is the object of StringBuilder class. Also, we can pass a string value(here “GeeksforGeeks”) 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("GeeksForGeeks", 20);
- 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)
- 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.
HELLO GFGGEEKS GeeksForGeeks
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.
Your total amount is Â¤50.00
StringBuilder.Insert(int index, string value) method
This method inserts the string at specified index in StringBuilder object.
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.
StringBuilder.Replace(old_val, new_val) Method
This method is used to replace characters within the StringBuilder object with another specified character.
Geeks For Geeks