C# String Remove() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, Remove() deletes characters from a string. With one argument it deletes from that position to the end. With two arguments it deletes a fixed number of characters starting at that position.

Like every string “editing” method, it returns a new string and leaves the original alone. To delete a known word instead of a position, either find it first with IndexOf() or use Replace(word, "").

This is an instance member of System.String. Call it on a string variable, such as text.Remove(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: Remove() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable.

Syntax

Give the start position, and optionally how many characters to delete.

C#
string text = "Hello, World!";

string a = text.Remove(5);       // "Hello"       (5 to the end removed)
string b = text.Remove(5, 7);    // "Hello!"      (7 characters removed)
CallWhat it does
text.Remove(5)Keep the first 5 characters; delete the rest.
text.Remove(0, 2)Delete the first 2 characters.
text.Remove(text.Length - 1)Delete the last character (text must not be empty).
text.Remove(i, word.Length)Delete a word you found at index i.

startIndex must be inside the string, and startIndex + count must not go past the end. Otherwise you get ArgumentOutOfRangeException. text.Remove(5) keeps the same characters as text.Substring(0, 5).

Library declarations
C#
public string Remove(int startIndex);
public string Remove(int startIndex, int count);

Parameters

ParameterDescription
startIndexRequired. The first position to delete. Must be at least 0 and less than Length (one-argument form).
countOptional. How many characters to delete. If you leave it out, everything to the end is deleted.

Return Value

Return ValueDescription
stringA new string without the removed characters.

C# Examples

Example 1 — Remove to the End vs Remove a Count

Compare the one-argument and two-argument forms on the same greeting.

C#
using System;

class Program {
    static void Main() {
        string text = "Hello, World!";

        Console.WriteLine(text.Remove(5));
        Console.WriteLine(text.Remove(5, 7));
        Console.WriteLine(text);
    }
}

How It Works

  1. Cut off the tail. Remove(5) deletes index 5 (the comma) and everything after it, leaving Hello.
  2. Cut a slice. Remove(5, 7) deletes exactly seven characters: the comma, the space, and World. The ! survives.
  3. Nothing was harmed. The third line prints the full original. Both calls created separate new strings.
  4. Pick by need. Use one argument to keep a prefix; use two arguments to cut a piece out of the middle.

Example 2 — Find a Word, Then Remove It

Locate the word really with IndexOf and remove exactly its length.

C#
using System;

class Program {
    static void Main() {
        string s = "I really like C#";
        string word = "really ";

        int i = s.IndexOf(word, StringComparison.Ordinal);
        Console.WriteLine(i);

        if (i != -1) {
            s = s.Remove(i, word.Length);
        }
        Console.WriteLine(s);
    }
}

How It Works

  1. Include the space. The search word is "really " with a trailing space, so removing it does not leave two spaces behind.
  2. Where it starts. I and a space take indexes 0 and 1, so the word starts at 2.
  3. Guard, then cut. The if skips the removal when the word is missing. word.Length (7) is the exact count to delete.
  4. Reassign. s = s.Remove(...) replaces the variable's value with the new string, so the next line prints I like C#.

Example 3 — Drop a Trailing Comma Safely

Remove the last character only when it is a comma, then see what happens when the count runs past the end.

C#
using System;

class Program {
    static void Main() {
        string csv = "red,green,blue,";

        if (csv.Length > 0 && csv[csv.Length - 1] == ',') {
            csv = csv.Remove(csv.Length - 1);
        }
        Console.WriteLine(csv);

        try {
            Console.WriteLine("abc".Remove(1, 5));
        } catch (ArgumentOutOfRangeException ex) {
            Console.WriteLine(ex.GetType().Name);
        }
    }
}

How It Works

  1. Check before cutting. The test makes sure the string is not empty and really ends with a comma.
  2. Last index. csv.Length - 1 is the position of the final character, and Remove with one argument deletes just that one.
  3. Too many. "abc" has only 2 characters after index 1, but the call asks for 5, so it throws.
  4. No partial result. Remove never deletes “as many as it can”. Compute a safe count, for example with Math.Min, if the length can vary.

Frequently Asked Questions

It returns a new string with characters deleted. Remove(i) deletes from i to the end. Remove(i, n) deletes n characters starting at i.
Use text = text.Remove(text.Length - 1); after checking that text is not empty. In C# 8 and later you can also write text[..^1].
Strings are immutable. Remove returns a new string, so assign it back: text = text.Remove(0, 1);
Find the position with IndexOf and call Remove(index, word.Length), or use Replace(word, "") to remove every copy.
ArgumentOutOfRangeException when startIndex or count is negative, or when they point past the end of the string.

Next: Replace()

Swap every copy of a character or substring for something else.

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.

12 people found this page helpful