In C#, StringBuilder.Remove(startIndex, length) deletes length characters, beginning at startIndex. The characters after the removed range slide left to close the gap, and Length goes down by the number removed.
It works by position, not by content: you say "cut 6 characters starting at index 7". To remove a known word wherever it appears, use Replace(word, ""). Unlike string.Remove, which returns a new string, this edits the builder in place and returns it for chaining.
An instance method of StringBuilder (using System.Text;), called as sb.Remove(...). The builder is a mutable buffer that changes in place, unlike an immutable string. It is not thread-safe.
Syntax
Pass where the cut starts and how many characters to remove.
Removes the last 2 characters, such as a trailing ", ".
sb.Remove(i, 1)
Removes the single character at index i.
sb.Remove(2, 0)
Allowed. Removes nothing.
startIndex and length must not be negative, and startIndex + length must not be larger than Length. Otherwise ArgumentOutOfRangeException is thrown and the builder is left unchanged. To empty the whole builder, Clear() is clearer than Remove(0, sb.Length).
Library declarations
C#
public StringBuilder Remove(int startIndex, int length);
Parameters
Parameter
Description
startIndex
Required. The position of the first character to remove (0-based).
length
Required. How many characters to remove. 0 removes nothing.
Return Value
Return Value
Description
StringBuilder
The same builder, shorter by length characters.
C# Examples
Example 1 — Cut a Word Out of the Middle
Remove the word cruel and its trailing space by position, then check the new length.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder sb = new StringBuilder("Hello, cruel World");
Console.WriteLine(sb.Length);
sb.Remove(7, 6);
Console.WriteLine(sb.ToString());
Console.WriteLine(sb.Length);
}
}
Output
18
Hello, World
12
How It Works
Find the positions. In Hello, cruel World, index 7 is the c of cruel, and cruel plus one space is 6 characters.
Cut by count.Remove(7, 6) deletes indexes 7 to 12, and World slides left to sit right after the comma and space.
Shorter text. The length drops from 18 to 12, exactly the 6 characters that were removed.
In place. No new builder or string was needed for the cut; sb itself now holds Hello, World.
Example 2 — Drop a Trailing Separator and Swap a Field
Build a list with a separator after every item, remove the extra one at the end, then replace a month by chaining Remove and Insert.
C#
using System;
using System.Text;
class Program {
static void Main() {
string[] tags = { "csharp", "dotnet", "text" };
StringBuilder sb = new StringBuilder();
foreach (string tag in tags) {
sb.Append(tag).Append(", ");
}
sb.Remove(sb.Length - 2, 2);
Console.WriteLine(sb.ToString());
StringBuilder date = new StringBuilder("2026-11-18");
date.Remove(5, 2).Insert(5, "12");
Console.WriteLine(date.ToString());
}
}
Output
csharp, dotnet, text
2026-12-18
How It Works
One separator too many. The loop adds ", " after every tag, including the last, so the text first ends with text,.
Trim the tail.Remove(sb.Length - 2, 2) cuts the final two characters, the comma and the space. (AppendJoin() avoids the extra separator entirely.)
Cut a field. In 2026-11-18 the month starts at index 5. Remove(5, 2) deletes 11, leaving 2026--18 for a moment.
Fill the gap. Because Remove returns the builder, .Insert(5, "12") runs straight after it and puts the new month in the same spot.
Example 3 — Keep Only the Digits of a Phone Number
Walk backwards through a phone number, remove every character that is not a digit, then try a range that runs past the end.
C#
using System;
using System.Text;
class Program {
static void Main() {
StringBuilder phone = new StringBuilder("(555) 123-4567");
for (int i = phone.Length - 1; i >= 0; i--) {
if (!char.IsDigit(phone[i])) {
phone.Remove(i, 1);
}
}
Console.WriteLine(phone.ToString());
Console.WriteLine(phone.Length);
try {
phone.Remove(8, 5);
} catch (ArgumentOutOfRangeException) {
Console.WriteLine("Remove range goes past the end");
}
}
}
Output
5551234567
10
Remove range goes past the end
How It Works
Read with the indexer.phone[i] returns the character at position i, and char.IsDigit tells whether it is 0 to 9.
Backwards is safe. Removing at i only moves characters to the right of i, which the loop has already checked. Going forwards would skip a character after each removal.
What is left. The brackets, the space, and the dash are gone, leaving 10 digits: 5551234567.
Range check. Removing 5 characters from index 8 would need 13 characters, but only 10 exist, so ArgumentOutOfRangeException is thrown and the digits are kept.
Frequently Asked Questions
It deletes a number of characters starting at a position, moves the rest of the text left, and returns the same builder.
No, it is a count. Remove(2, 3) removes three characters: the ones at indexes 2, 3, and 4.
Call sb.Remove(sb.Length - 1, 1), after checking that sb.Length is greater than 0. You can also set sb.Length = sb.Length - 1.
Use Replace: sb.Replace("word", "") removes every occurrence of the word. Remove only works with positions.
If startIndex + length is larger than Length, or either number is negative, Remove throws ArgumentOutOfRangeException and does not change the builder.