C# Tutorial For Beginners: The String Builder Class
Updated: Aug 22, 2021
The “string builder” class provides a better more efficient way to handle stings in our program. By using this class, we can reduce memory allocation that is used in case of changing string after creation (String is a ) ‘Mutable’, and therefore any modification will increase the memory allocation.
To use a “String Builder” class, we first need to create a new instance of the class, using one of the following options:
class Program
{
static void Main(string[] args)
{
//Example 1: Creating instance without initialization
StringBuilder Example_1 = new StringBuilder("String X");
//Example 2: Creating an instance and initialize it with a default value
StringBuilder Example_2 = new StringBuilder("String X");
//Example 3: Creating an instance with expected string length
StringBuilder Example_3 = new StringBuilder(5);
Console.ReadKey();
}
}
Builder Properties and Methods
Similar to any other class, the string builder class has its unique properties and methods. let's start to examine and demonstrate how can we use them in our code.
Append, AppendLine, and AppendFormat methods
These methods are used to add additional text to an initialized builder.
Example 1: Using the Append method to add syntax without moving to the next line
static void Main(string[] args)
{
StringBuilder PhoneNumber = new StringBuilder("My Name is : ");
//Adding values directly (Option 1)
PhoneNumber.Append("David\n");
PhoneNumber.Append("My Phone number: ");
//Adding values using loop (Option 2)
for (int i = 0; i < 9; i++)
{
PhoneNumber.Append(i);
}
Console.WriteLine(PhoneNumber);
Console.ReadKey();
}
Result:
My Name is: David
My Phone number: 012345678
Example 2: Using the Append Line method to add syntax and start a new line
class Program
{
static void Main(string[] args)
{
StringBuilder PhoneNumber = new StringBuilder("My Name is : ");
//Adding values directly (Option 1)
PhoneNumber.AppendLine("David");
PhoneNumber.AppendLine("My Phone number: ");
//Adding values using loop (Option 2)
for (int i = 0; i < 9; i++)
{
PhoneNumber.Append(i);
}
Console.WriteLine(PhoneNumber);
}
}
Result:
My Name is: David
My Phone number:
012345678
Example 3: Using the AppendFormat method to modify a specific part of the builder
static void Main(string[] args)
{
string BestMovieEver = "Rambo";
string BestTvShow = "Frainds";
StringBuilder FormatBuilder = new StringBuilder();
FormatBuilder.AppendFormat
(@"{0} is the Best Movie Ever!!!
{1} is the best Tv Show", BestMovieEver, BestTvShow);
Console.WriteLine(FormatBuilder);
}
Result:
Rambo 2 is the Best Movie Ever!!!
Friends is the best Tv Show!!!
Replace method
The replace method is used to change specific parts of a string.
static void Main(string[] args)
{
//Example 1:
StringBuilder ReplaceValue = new StringBuilder("My name is : None");
ReplaceValue.Replace("None", "David .T.");
Console.WriteLine("Example 1 : " + ReplaceValue);
//Example 2:
StringBuilder ReplaceMultipleValues = new StringBuilder("0-1-0-1-0-1-0");
ReplaceMultipleValues.Replace("0", "2");
Console.WriteLine("Example 2 : " + ReplaceMultipleValues);
}
Result:
Example 1: My name is: David .T.
Example 2: 2-1-2-1-2-1-2
Clear method
The ‘Clear’ method will clear the builder data.
static void Main(string[] args)
{
StringBuilder clearvalues = new StringBuilder("1984");
Console.WriteLine("Before : " + clearvalues);
clearvalues.Clear();
Console.WriteLine("After : " + clearvalues);
}
Result:
Before: 1984
After:
Remove method
This method is used to remove a specific range of characters based on builder indexes.
static void Main(string[] args)
{
StringBuilder Remove = new StringBuilder("123456789");
Console.WriteLine("Before : " + Remove);
Remove.Remove(6,1);
//Starting the remove from index 6
//Remove 1 character from index 6
Console.WriteLine("After : " + Remove);
}
Result:
Before: 123456789
After: 1234569