C-SHARP - 4.9 Stringbuilder (Demo)

This demo puts StringBuilder into practice. After adding using System.Text; at the top of the file, we create the builder with var builder = new StringBuilder();. Then we explore the most useful methods. Append has 26 overloads accepting strings, chars, ints, doubles and arrays. We use the overload taking a character and a count to repeat a symbol: builder.Append('*', 10); writes ten stars in a row. AppendLine() adds an empty line for nicer console output.

We then write a small algorithm asking the user whether they want to replace the stars with dashes. We read their answer with Console.ReadLine(), then call builder.Replace('*', '-') only if they typed yes. The Remove method takes a starting index and a length to delete; builder.Remove(0, 5) drops the first five characters. The Insert method takes the position and the content to inject; you can insert a string repeated n times by passing new string('+', 5).

using System.Text;

var builder = new StringBuilder("Bonjour a tous")
    .Append('*', 10)
    .AppendLine()
    .Append("Welcome")
    .Replace('*', '-')
    .Remove(0, 5)
    .Insert(0, new string('+', 5));

Console.WriteLine(builder);
Console.WriteLine("First char: " + builder[0]);

Useful tips

  • Pass an initial string to the constructor: new StringBuilder("Bonjour");.
  • Access characters by index using brackets: builder[0] returns the first character.
  • Most methods return the StringBuilder itself, so you can chain calls fluently.
  • StringBuilder does not provide search methods such as IndexOf.

Because every method returns the same builder, we can chain operations to keep the code clean: builder.Append("...").Replace(...).Remove(...).Insert(...). This fluent style is more readable than calling each method on its own line. Combined with the operations we already know on regular strings, StringBuilder gives you the right tool for performance-sensitive string manipulation. See you in the next video.

Summary

This lesson provides a hands-on demonstration of the StringBuilder class in C# using Visual Studio. You'll learn how to instantiate StringBuilder, append strings and characters efficiently, and manipulate strings using key methods like Append(), Replace(), and Remove(). The demo shows practical use cases such as creating formatted headers and dynamically modifying string content based on user input.

Key points

  • StringBuilder is a mutable string class contained in System.Text, ideal for building and manipulating strings in loops or complex operations
  • The Append() method has 26 overloads supporting different parameter types (string, char, char[], double, int, etc.), allowing flexible string construction
  • Append() can repeat characters by passing a char and repeat count, enabling efficient generation of formatted patterns like repeated asterisks or dashes
  • The Replace() method swaps characters or substrings within the StringBuilder with new values, supporting conditional modifications based on user input
  • The Remove() method deletes characters from a specified index position for a given length, enabling substring deletion operations
  • StringBuilder improves performance compared to string concatenation, particularly useful for building complex formatted output or generating dynamic content

FAQ

Why should I use StringBuilder instead of string concatenation in C#?

StringBuilder is more efficient than repeated string concatenation because strings are immutable in C#. Each concatenation creates a new string object in memory. StringBuilder allows you to build complex strings with multiple operations on a single, mutable object, significantly improving performance especially in loops or when building large formatted outputs.

How do I import and use StringBuilder in Visual Studio?

StringBuilder is located in the System.Text namespace. To use it, right-click on the unresolved reference in Visual Studio and select to import the System.Text class. Then instantiate it with 'var builder = new StringBuilder();' and use its methods to append, replace, or remove characters and strings.

What are practical use cases for StringBuilder in real projects?

StringBuilder is ideal for generating formatted output (like headers or reports), building dynamic word generators, constructing SQL queries programmatically, and any scenario where you need to build strings through multiple operations. It excels in loops where concatenation would otherwise create numerous temporary string objects in memory.