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
- How to create the StringBuilder in C#
- C# | String vs StringBuilder
- How to find the MaxCapacity of a StringBuilder in C#
- How to find the length of the StringBuilder in C#
- How to find the Capacity of a StringBuilder in C#
- StringBuilder.EnsureCapacity() Method in C#
- StringBuilder.CopyTo Method in C#
- C# | Creating StringBuilder having specified capacity
- StringBuilder.Chars Property in C#
- StringBuilder.ToString Method in C#
- How to remove all characters from StringBuilder in C#
- C# | Check if two StringBuilder objects are Equal
- Kotlin constructor
- Parameterless Method in Scala
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.