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)
Call
What 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
Parameter
Description
startIndex
Required. The first position to delete. Must be at least 0 and less than Length (one-argument form).
count
Optional. How many characters to delete. If you leave it out, everything to the end is deleted.
Return Value
Return Value
Description
string
A 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);
}
}
Output
Hello
Hello!
Hello, World!
How It Works
Cut off the tail.Remove(5) deletes index 5 (the comma) and everything after it, leaving Hello.
Cut a slice.Remove(5, 7) deletes exactly seven characters: the comma, the space, and World. The ! survives.
Nothing was harmed. The third line prints the full original. Both calls created separate new strings.
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);
}
}
Output
2
I like C#
How It Works
Include the space. The search word is "really " with a trailing space, so removing it does not leave two spaces behind.
Where it starts.I and a space take indexes 0 and 1, so the word starts at 2.
Guard, then cut. The if skips the removal when the word is missing. word.Length (7) is the exact count to delete.
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.