StringBuilder helps you build and change text quickly without making many copies. It is faster than using normal text when you change text many times.
StringBuilder methods and performance in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
StringBuilder sb = new StringBuilder(); sb.Append("text"); sb.Insert(index, "more text"); sb.Remove(startIndex, length); sb.Replace("old", "new"); string result = sb.ToString();
StringBuilder is in the System.Text namespace, so you need using System.Text; at the top.
Use ToString() to get the final text from StringBuilder.
Examples
C Sharp (C#)
StringBuilder sb = new StringBuilder(); sb.Append("Hello"); sb.Append(" World"); string result = sb.ToString();
C Sharp (C#)
StringBuilder sb = new StringBuilder("Start"); sb.Insert(5, " Here"); string result = sb.ToString();
C Sharp (C#)
StringBuilder sb = new StringBuilder("Hello World"); sb.Replace("World", "C#"); string result = sb.ToString();
C Sharp (C#)
StringBuilder sb = new StringBuilder("Hello C#"); sb.Remove(5, 3); string result = sb.ToString();
Sample Program
This program builds a greeting step-by-step using StringBuilder methods. It shows how Append, Insert, Replace, and Remove work together.
C Sharp (C#)
using System; using System.Text; class Program { static void Main() { StringBuilder sb = new StringBuilder(); sb.Append("Hello"); sb.Append(", "); sb.Append("world"); sb.Insert(5, " dear"); sb.Replace("world", "C#"); sb.Remove(10, 1); // remove space after 'dear' string result = sb.ToString(); Console.WriteLine(result); } }
Important Notes
StringBuilder is much faster than normal string when changing text many times.
Use StringBuilder when you have loops that add or change text repeatedly.
Remember to call ToString() to get the final text.
Summary
StringBuilder helps build text efficiently by changing it without making copies.
Use methods like Append, Insert, Replace, and Remove to change text.
Always convert to string with ToString() when done.
Practice
1. What is the main advantage of using
StringBuilder over regular string concatenation in C#?easy
Solution
Step 1: Understand string immutability in C#
Regular strings cannot be changed once created, so concatenation creates new strings each time.Step 2: How StringBuilder works
StringBuilder changes the text in place without making new copies, which is faster for many changes.Final Answer:
It modifies the string without creating new string copies, improving performance. -> Option AQuick Check:
StringBuilder avoids new copies [OK]
Hint: StringBuilder changes text without copying strings [OK]
Common Mistakes:
- Thinking StringBuilder sorts or encrypts text
- Believing StringBuilder changes case automatically
- Confusing StringBuilder with regular string methods
2. Which of the following is the correct way to append text to a
StringBuilder named sb?easy
Solution
Step 1: Recall StringBuilder methods
Append is the method used to add text at the end of the current content.Step 2: Check method names
Add and Concat are not valid StringBuilder methods; Insert adds text at a specific position, not at the end.Final Answer:
sb.Append("Hello"); -> Option AQuick Check:
Append adds text at end [OK]
Hint: Use Append() to add text at the end [OK]
Common Mistakes:
- Using Add() which does not exist
- Confusing Insert() with Append()
- Trying to use Concat() on StringBuilder
3. What will be the output of the following C# code?
var sb = new System.Text.StringBuilder("Hi");
sb.Append(" there");
sb.Replace("Hi", "Hello");
sb.Remove(5, 1);
Console.WriteLine(sb.ToString());medium
Solution
Step 1: Trace Append and Replace
Start with "Hi", Append adds " there" -> "Hi there". Replace "Hi" with "Hello" -> "Hello there".Step 2: Apply Remove
Remove(5,1) removes 1 character at index 5 (0-based). Index 5 is the space between "Hello" and "there", so removing it joins words -> "Hellothere".Final Answer:
Hellothere -> Option CQuick Check:
Remove space at index 5 = "Hellothere" [OK]
Hint: Remember Remove(index, count) deletes characters at index [OK]
Common Mistakes:
- Forgetting zero-based index in Remove
- Assuming Replace changes all occurrences incorrectly
- Not converting StringBuilder to string before printing
4. Identify the error in this code snippet:
var sb = new System.Text.StringBuilder();
sb.Append("Start");
sb.Remove(10, 3);
Console.WriteLine(sb.ToString());medium
Solution
Step 1: Check Remove parameters
Remove(10, 3) tries to remove 3 characters starting at index 10, but current string length is 5 ("Start").Step 2: Understand exception
Removing beyond string length causes ArgumentOutOfRangeException at runtime.Final Answer:
Remove method call will throw an ArgumentOutOfRangeException. -> Option DQuick Check:
Remove index out of range = Exception [OK]
Hint: Check Remove index is within current length [OK]
Common Mistakes:
- Assuming Remove silently ignores invalid indexes
- Thinking Append is incorrect here
- Believing ToString needs no parentheses
5. You want to build a comma-separated list of numbers from 1 to 5 using
StringBuilder. Which code snippet is the most efficient and correct?hard
Solution
Step 1: Appending comma after each number then removing trailing
Appends number and comma each time, then removes last comma. Remove call adds overhead.Step 2: Conditional comma append
Appends number, then comma only if not last number. Avoids extra Remove call, more efficient and clear.Step 3: Analyze options B and C
B appends comma after last number, no removal, so extra comma remains. C hardcodes string, no loop, less flexible.Final Answer:
sb.Append(i); if(i < 5) sb.Append(","); -> Option BQuick Check:
Append comma conditionally [OK]
Hint: Add comma only between items, not after last [OK]
Common Mistakes:
- Leaving trailing comma without removal
- Hardcoding string instead of looping
- Removing characters unnecessarily
