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.
// C# program to illustrate the// public string Remove(int StartIndex)usingSystem;classGeeks {// Main MethodpublicstaticvoidMain(){// define stringString str ="GeeksForGeeks";Console.WriteLine("Given String : "+ str);// delete from index 5 to end of stringConsole.WriteLine("New String1 : "+ str.Remove(5));// delete character from index 8 to end of stringConsole.WriteLine("New String2 : "+ str.Remove(8));}}chevron_rightfilter_noneOutput: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.
// C# program to illustrate the// public string Remove(int StartIndex, int count)usingSystem;classGeeks {// Main MethodpublicstaticvoidMain(){// original stringString str ="GeeksForGeeks";Console.WriteLine("Given String : "+ str);// delete the string from index 2 to length 4Console.WriteLine("New String1 : "+ str.Remove(2, 4));// delete the string from index 5 to length 3Console.WriteLine("New String2 : "+ str.Remove(5, 3));}}chevron_rightfilter_noneOutput: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
Recommended Posts:
- C# | Dictionary.Remove Method
- C# | SortedDictionary.Remove() Method
- C# | Remove all the strings from the StringCollection
- C# | Remove the first occurrence from the StringCollection
- C# | Remove all elements from the SortedSet
- C# | Remove all elements from the ArrayList
- C# | Remove the element with the specified key from a SortedList
- C# | Remove all elements from a SortedList
- C# | Remove from the specified index of a SortedList
- C# | Remove entry with specified key from the StringDictionary
- C# | Remove all objects from the Stack
- C# | Remove all elements from the Collection<T>
- C# | Remove all elements from a HashSet
- C# | Remove entry with specified key from OrderedDictionary
- C# | Remove all elements from OrderedDictionary
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.



