C# | Remove() Method

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

Syntax:

public string Remove(int StartIndex)  
or
public string Remove(int StartIndex, int count)  

Explanation:
public string Remove(int StartIndex) method will take a single parameter which is the starting index or we can say the specified position from where it will start to remove characters from the current String object. And this method will continue to remove the characters till the end of the current string object.

public string Remove(int StartIndex, int count) method will take two arguments i.e first is start position of specified string and the second one is the number of characters to be removed. The return type value of both the methods is System.String.

Exceptions: There can be two cases where exception ArgumentOutOfRangeException may occur are as follows:

  • Either StartIndex or (StartIndex + count) indicates a position which may outside the current string object.
  • StartIndex or count is less than zero.

Below are the programs to demonstrate the above Methods :

  • Example 1: Program to demonstrate the public string Remove(int StartIndex) method. The Remove method will removes all the characters from the specified index till the end of the string.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the
    // public string Remove(int StartIndex)
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // define string
            String str = "GeeksForGeeks";
      
            Console.WriteLine("Given String : " + str);
      
            // delete from index 5 to end of string
            Console.WriteLine("New String1 : " + str.Remove(5));
      
            // delete character from index 8 to end of string
            Console.WriteLine("New String2 : " + str.Remove(8));
        }
    }

    chevron_right

    
    

    Output:

    Given String : GeeksForGeeks
    New String1 : Geeks
    New String2 : GeeksFor
    
  • Example 2: Program to demonstrate the public string Remove(int StartIndex, int count) method. This method will remove the characters from the specified index to specified index + (count – 1) of the string where count is the number of the characters to be removed.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // C# program to illustrate the
    // public string Remove(int StartIndex, int count)
    using System;
      
    class Geeks {
      
        // Main Method
        public static void Main()
        {
      
            // original string
            String str = "GeeksForGeeks";
      
            Console.WriteLine("Given String : " + str);
      
            // delete the string from index 2 to length 4
            Console.WriteLine("New String1 : " + str.Remove(2, 4));
      
            // delete the string from index 5 to length 3
            Console.WriteLine("New String2 : " + str.Remove(5, 3));
        }
    }

    chevron_right

    
    

    Output:

    Given String : GeeksForGeeks
    New String1 : GeorGeeks
    New String2 : GeeksGeeks
    

Important Point to Remember:

  • Both above methods do not modify the value of the current string object. Instead, they return a new modified string.
  • If StartIndex is equal to the length of string and length is zero, the method will not remove any character from the string.

References:
https://msdn.microsoft.com/en-us/library/system.string.remove1

https://msdn.microsoft.com/en-us/library/system.string.remove2



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.