C# String - Remove() Method



The C# String Remove() method is used to remove specified characters from a string and returns a new string.

The characters you want to remove depend on the starting index and the count. The starting index represent the position in the string from which you want to begin removing characters, while the count specifies the total number of characters to be removed.

Syntax

Following is the syntax of the C# string Remove() method −

public string Remove (int startIndex, int count);

Parameters

This method accepts the following parameters −

  • startIndex: It represents the position from which you want to remove.
  • count: It represents the number of characters you want to remove from the string.

Return Value

This method returns a new string. That is equal to the current string except for the removed characters.

Example 1: Remove Character from Start Index to the End

Following is a basic example of the Remove() method to remove the characters from the start index to the end of the string −

    
using System;
class Program {
   static void Main() {
      string original = "tutorialspoint, India";
      string result = original.Remove(5);
      Console.WriteLine(result);
   }
}

Output

Following is the output −

tutor

Example 2: Remove a Specific Number of Characters

Let's look at another example. Here, we use the Remove() method to remove the specific number of characters from the start index in the string −

using System;
class Program {
   static void Main() {
      string original = "tutorialspoint, India";
      string result = original.Remove(14, 7);
      Console.WriteLine(result);
   }
}

Output

Following is the output −

tutorialspoint

Example 3: Attempt to Remove Beyond the String Length

In this example, we use the Remove() method to delete a specified number of characters from the string. If the specified length exceeds the string's actual length, we receive an ArgumentOutOfRangeException.exception −

using System;
class Program {
  static void Main() {
    string original = "Hello, tpians!";
    try {
      string result = original.Remove(7, 20);
      Console.WriteLine(result);
    } catch (ArgumentOutOfRangeException ex) {
      Console.WriteLine("Error: " + ex.Message);
    }
  }
}

Output

Following is the output −

Error: Index and count must refer to a location within the string.
Parameter name: count

Example 4: Remove Domain from an Email Address

In this example, we use the Remove() method to remove the domain part (every character after the @ symbol) from an email address −

using System;
class Program { 
   static void Main() {
      string email = "[email protected]";
      
      // Find the index of '@'
      int atIndex = email.IndexOf('@');

      // Remove the domain part if '@' is found
      string username = atIndex >= 0 ? email.Remove(atIndex) : email;

      Console.WriteLine("Original Email: " + email);
      Console.WriteLine("Username Only: " + username); 
   }
}

Output

Following is the output −

Original Email: [email protected]
Username Only: amangupta123
csharp_strings.htm
Advertisements