String Builder in .NET C#: Usage, and Example

Introduction

Since the string type in C# is immutable, we are unable to alter its contents once they are created. This implies that attempting to modify a string-type object after it has been created will result in the creation of a new instance of the object in memory. Additionally, there may be performance issues if the string is changed frequently.

This issue is resolved by StringBuilder as, in contrast to string, it has the ability to dynamically expand its memory to accommodate any type of manipulation.

The right way to create a StringBuilder

A StringBuilder is rather simple to instantiate. Using the new keyword to create an object of a class, we may instantiate it. Furthermore, a StringBuilder has other constructors.

StringBuilder

StringBuilder to String Conversion

For any kind of string manipulation, we can utilize a StringBuilder. However, a string is not returned by the StringBuilder. Therefore, we need to utilize the ToString() method in order to retrieve a string.

var sb = new StringBuilder("Jaimin Shethiya");

var name = sb.ToString();

The content is first converted to a string and then a StringBuilder is created with a default text.

StringBuilder Techniques

We can work with a StringBuilder's contents in a few different ways. These include Replace(), Insert(), Clean(), Remove(), Append(), AppendLine(), and AppendFormat().

Append

A new string is appended to the end of the existing StringBuilder using the Append() function. The StringBuilder's length can be doubled, and space allocation happens automatically.

var sb = new StringBuilder("Hello, ");
sb.Append("Jaimin Shethiya");
var name = sb.ToString();

Console.WriteLine("{0} - {1}", nameof(name), name);

Append

AppendLine

AppendLine() is useful when we want to add a line terminator to our StringBuilder. To accomplish this, we can make a StringBuilder and use the following method.

var sb = new StringBuilder("Hello, ");
sb.Append("Jaimin Shethiya");
sb.AppendLine();
sb.AppendLine("How are you?");

Console.WriteLine(sb.ToString());

AppendLine

AppendFormat

AppendFormat() appends a string to the end of StringBuilder in a predetermined format. The conventions of either the chosen culture or the current system culture are revealed in the produced string. The method lets us pass the desired format for our string as an input.

var sb = new StringBuilder("Hello, ");
sb.Append("Jaimin Shethiya");
var text = "C# Corner Rank";
var rank = 120;
sb.AppendLine();
sb.AppendFormat("{0} - {1}", text, rank);

Console.WriteLine(sb.ToString());

AppendFormat

Insert

This method adds, to our StringBuilder object, a string, substring, character array, part of a character array, or the string representation of a primitive data type at a certain place. We may also add the index for our insertion using this method.

var sb = new StringBuilder("Hello");
var name = ", Jaimin Shethiya";
sb.Insert(5, name);

Console.WriteLine(sb.ToString());

Insert

Replace

Several character occurrences in the StringBuilder object are replaced by this method. The desired character sequence and a new value are entered into the method as inputs.

var sb = new StringBuilder("Hello");
var name = ", Jaimin Shethiya";
sb.Insert(5, name);
Console.WriteLine("Orioginal data: {0}", sb.ToString());
Console.WriteLine();

sb.Replace("Jaimin", "Jacky");

Console.WriteLine("Replace data: {0}", sb.ToString());

Replace

Remove

A predetermined amount of characters are removed from the StringBuilder using the Remove() function. The start index and the number of characters to be deleted are the input parameters.

var sb = new StringBuilder("Hello");
sb.Append(", Jaimin Shethiya");

Console.WriteLine("Orioginal data: {0}", sb.ToString());
Console.WriteLine();

sb.Remove(0, 7);

Console.WriteLine("Remove data: {0}", sb.ToString());

Remove

The Remove technique eliminates 7 characters starting at index 0.

Clear

The StringBuilder object's characters are all eliminated using this function. This method reduces the length of a StringBuilder to zero.

var sb = new StringBuilder("Hello");
sb.Append(", Jaimin Shethiya");

Console.WriteLine("Orioginal data: {0}", sb.ToString());
Console.WriteLine();

sb.Clear();

Console.WriteLine("Clear data: {0}", sb.ToString());

Clear

We learned the new technique and evolved together.

Happy coding!


Similar Articles