C# StringBuilder Remove() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
StringBuilder

Definition

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.

C#
StringBuilder sb = new StringBuilder("abcdef");
sb.Remove(1, 2);              // adef
sb.Remove(sb.Length - 1, 1);  // ade
sb.Remove(0, sb.Length);      // "" (everything)
CallWhat it does
sb.Remove(0, 3)Removes the first 3 characters.
sb.Remove(sb.Length - 2, 2)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

ParameterDescription
startIndexRequired. The position of the first character to remove (0-based).
lengthRequired. How many characters to remove. 0 removes nothing.

Return Value

Return ValueDescription
StringBuilderThe 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);
    }
}

How It Works

  1. Find the positions. In Hello, cruel World, index 7 is the c of cruel, and cruel plus one space is 6 characters.
  2. Cut by count. Remove(7, 6) deletes indexes 7 to 12, and World slides left to sit right after the comma and space.
  3. Shorter text. The length drops from 18 to 12, exactly the 6 characters that were removed.
  4. 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());
    }
}

How It Works

  1. One separator too many. The loop adds ", " after every tag, including the last, so the text first ends with text,.
  2. Trim the tail. Remove(sb.Length - 2, 2) cuts the final two characters, the comma and the space. (AppendJoin() avoids the extra separator entirely.)
  3. Cut a field. In 2026-11-18 the month starts at index 5. Remove(5, 2) deletes 11, leaving 2026--18 for a moment.
  4. 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");
        }
    }
}

How It Works

  1. Read with the indexer. phone[i] returns the character at position i, and char.IsDigit tells whether it is 0 to 9.
  2. 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.
  3. What is left. The brackets, the space, and the dash are gone, leaving 10 digits: 5551234567.
  4. 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.

Next: Replace()

Swap every occurrence of a string or char for another one, in the whole text or a range.

Replace() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful