C# String Remove() Method (original) (raw)

Last Updated : 10 Mar, 2025

In C#, the Remove() method of the String class is used for removing the characters from the specified position of a string. If the length is not specified, then it will remove all the characters after the specified position. This method can be overloaded by changing the number of arguments passed to it.

**Example 1: Using String.Remove() method to remove the specified characters from a string.

C# `

// C# program to illustrate the // String.Remove Method using System;

class Geeks { public static void Main(String[] args) {
// Creating a string string s = "GeeksforGeeks";

    // After using Remove(Int32) Method
    string res = s.Remove(5);
     
     // Original string
    Console.WriteLine("Original String: " + s);

    Console.WriteLine("Result: " + res);

}

}

`

Output

Original String: GeeksforGeeks Result: Geeks

**Explanation: In the above example, we use the Remove(int StartIndex) method which takes a single parameter as the starting index it will start to remove characters from the current String object. This method will continue to remove the characters till the end of the current string object.

Syntax of String Remove() Method

public string Remove(int StartIndex)

public string Remove(int StartIndex, int count)

**Parameters:

**Return Type:

**Exceptions: There can be two cases where exception **ArgumentOutOfRangeException may occur

**Example 2: Using the Remove(int StartIndex) method to remove all the characters from the start index of a string.

C# `

// C# program to illustrate the // public string Remove(int StartIndex) using System;

class Geeks { public static void Main() {

    // define string 
    String s = "GeeksForGeeks"; 

    Console.WriteLine("Original String : " + s); 

    // delete from index 4 to end of string 
    Console.WriteLine("New String1 : " + s.Remove(4)); 

    // delete character from index 5 to end of string 
    Console.WriteLine("New String2 : " + s.Remove(5)); 
} 

}

`

Output

Original String : GeeksForGeeks New String1 : Geek New String2 : Geeks

**Explanation: In the above example, we use the Remove(int StartIndex ) method and specified the StartIndex from which we want to delete the characters till the end.

**Example 3: Using Remove(int StartIndex, int count) method with the count of characters to remove.

C# `

// C# program to illustrate the // public string Remove(int StartIndex, int count) using System;

class Geeks { public static void Main() { // original string String s = "GeeksForGeeks";

    Console.WriteLine("Given String : " + s); 

    // Remote character from index 5
    // and count 3
    String res = s.Remove(5, 3);
    System.Console.WriteLine("After Remove : " + res);

    // Remote character from index 5
    // and count 5
    res = s.Remove(0, 5);

    System.Console.WriteLine("After Remove : " + res);
} 

}

`

Output

Given String : GeeksForGeeks After Remove : GeeksGeeks After Remove : ForGeeks

**Explanation: In the above example, we use the Remove(startIndex, count) method and first we specify the index from which we want to remove the characters and the count of the number of characters to remove.

**Important Points: