In c#, StringBuilder is a class that is useful to represent a mutable string of characters, and it is an object of the System.Text namespace.
Like string in c#, we can use a StringBuilder to create variables to hold any text, a sequential collection of characters based on our requirements.
In c#, both string and StringBuilder will represent a sequence of characters and perform the same kind of operations, but the only difference is strings are immutable, and StringBuilder is mutable.
Generally, in c# the string object cannot be modified once it is created. If any changes are made to the string object, like adding or modifying an existing value, it will simply discard the old instance in memory and create a new instance to hold the new value. If we are doing repeated modifications on the string object, it will affect the application's performance. To know more about strings, check strings in c# with examples.
To solve this problem, c# introduced an alternative called StringBuilder, which is a mutable string class. Mutability means once an instance of the class is created, then the same instance will be used to perform any operations like inserting, appending, removing, or replacing the characters instead of creating a new instance every time.
In c#, the StringBuilder is a dynamic object which will expand a memory dynamically to accommodate the modifications of string instead of creating a new instance in the memory.
Following is the pictorial representation of memory allocation for StringBuilder object in c# programming language.
As discussed, the StringBuilder class is an object of the System.Text namespace, so to use StringBuilder in our application, we need to import the System.Text namespace.
In c#, the StringBuilder declaration and initialization will be the same as the class. The following are the different ways of declaring and initializing a stringbuilder in the c# programming language.
If you observe the above code snippet, we created an instance of the StringBuilder class by defining our variable with overloaded constructor methods.
As discussed, the StringBuilder is a dynamic object which will expand dynamically to accommodate the number of characters based on the string modifications. We can also specify an initial capacity of characters, the StringBuilder can hold by passing an int value using one of the overloaded constructors or StringBuilder Capacity property.
For example, we created a StringBuilder by specifying the capacity of 25 characters and appending a string whose length is greater than the capacity of 25 characters. In this case, the new space will be allocated automatically, and the capacity of StringBuilder will be doubled.
Following is the example of specifying the initial capacity of characters the StringBuilder can hold in the c# programming language.
In c#, whenever the defined capacity of StringBuilder is lesser than the appended string value, then the current capacity of StringBuilder automatically will increase to match the appended string value.
The default capacity of StringBuilder is 16 characters, and its maximum capacity is more than 12 billion characters.
The following table lists the important StringBuilder methods, which we can use to modify the contents of StringBuilder.
Method | Description |
---|---|
StringBuilder.Append | This method will append the given string value to the end of the current StringBuilder. |
StringBuilder.AppendFormat | It will replace a format specifier passed in a string with formatted text. |
StringBuilder.Insert | It inserts a string at the specified index of the current StringBuilder. |
StringBuilder.Remove | It removes a specified number of characters from the current StringBuilder. |
StringBuilder.Replace | It replaces a specified character at a specified index. |
The Append method is used to add or append a string object at the end of the string represented by the StringBuilder.
Following is the example of initializing a StringBuilder with some text and appending a required text at the end of the string object.
The AppendFormat method is used to add or append string objects by formatting them into a specified format at the end of the string represented by the StringBuilder.
Following is the example of initializing a StringBuilder with some text and appending a formatted text at the end of the string object.
The Insert method is used to insert a string at the specified index position of the current StringBuilder object.
Following is an example of initializing a StringBuilder with some text and inserting a string at the specified index position of the StringBuilder object.
The Remove method is used to remove a specified number of characters from the current StringBuilder object, starting from the specified index position.
Following is the example of removing a specified number of characters from the StringBuilder object, starting from the specified index position.
The Replace method is used to replace all occurrences of specified string characters in the current StringBuilder object with a specified replacement string character.
Following is the example of replacing a specified number of characters from the StringBuilder object with specified replace characters.
In c#, the StringBuilder has another method called AppendLine(), which is used to add a new line at the end of the string.
Following is the example of using StringBuilder to insert or append or replace or remove a particular string text in c# programming language.
If you observe the above code, to use StringBuilder in our application, we imported a System.Text namespace and used different methods of StringBuilder to make required modifications based on our requirements.
When you execute the above c# program, you will get the result as shown below.
This is how we can use StringBuilder in our applications to make the required modifications to the string objects based on our requirements.
In c#, you can convert a StringBuilder object to a string by calling StringBuilder.ToString() method.
Following is the example of converting a StringBuilder object to a string using the ToString() method in c# programming language.
If you observe the above code, we are converting the StringBuilder object (sb) to a string object using sb.ToString() method.
When you execute the above c# program, you will get the result below.
This is how we can convert the StringBuilder object to a string based on our requirements.
The following are the important points we need to remember while using a StringBuilder in the c# programming language.