The Difference between String and StringBuilder in C#

AramT
54.5K views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

The StringBuilder class provides the developer with a set of methods that allows manipulation of a mutable string of characters, it can be used when you want to modify a string without creating a new object, thus eliminating the overhead or bottleneck issue of the normal String concatenation.

Using the StringBuilder class can boost performance when concatenating many strings together in a loop.

To use StringBuilder class, you need to make sure you are using the namespace System.Text

The following is a list of a few operations that can be performed to manipulate a string using the StringBuilder Class in .NET

  • Append: Appends information to the end of the current StringBuilder.

  • AppendLine: Appends information as well as the default line terminator to the end of the current StringBuilder.

  • AppendFormat: Replaces a format specifier passed in a string with formatted text.

  • Insert: Inserts a string or object into the specified index of the current StringBuilder.

  • Remove: Removes a specified number of characters from the current StringBuilder.

  • Replace: Replaces a specified character at a specified index.

For the complete list of available methods in StringBuilder class, please refer to Microsoft's documentation here

Open Source Your Knowledge: become a Contributor and help others learn. Create New Content