Advertisement



< Prev
Next >



C# Remove() method




As the name of the method says, the Remove() method of String class is used to return a copy of the invoked String after removing a specified number of characters starting from a specified index in the invoked String, let's discuss the two overloads of this method.




Some overloads of Remove() method



Methods Description
Remove(int startIndex)
This method returns a copy of the invoked String by removing the characters in it, starting at the startIndex and ending at the last index of the invoked String object.

Remove(int startIndex, int length)
This method returns a copy of the invoked String by removing the characters in it, starting at the startIndex and length specifies the total number of characters to be removed from the invoked String object value.






Note :


The startIndex int value is a zero-based index, hence the first index is counted as 0.




An example of Remove(int startIndex) metho


Here in program below, we are going to explain the first overload version of the Remove(int startIndex) method.

//C# Example of Remove(int startIndex) method.


using System;

class StringRemoveEx
{
public static void Main()
{
	//Creating a String object
	String str1= "0123456789";

	//Calling the Remove() method on the String object
	//to remove all characters starting from index 3
	String str2  = str1.Remove(3);

	//Printing the original String
	Console.WriteLine("Original String is "+ str1);
	
	//Printing the extracted substring
	Console.WriteLine("String returned after removing characters from index 3 to its end: "+ str2);
}
}

Output is :

Original String is 0123456789
String returned after removing characters from index 3 to its end: 012


Program Analysis




Note: Because startIndex is an zero-based index int value, that's why the first index of a string is counted as 0.


Advertisement




Example of Remove(int startIndex, int length)


In the next example, we are explaining the second form of Remove() method i.e. Remove(int startIndex, int length) method, which returns a copy of the invoked String by removing the characters in it, starting at the startIndex and length specifies the total number of characters to be removed from the invoked String object value.

//C# Example of Remove(int startIndex, int length)


using System;

class StringRemove
{
public static void Main()
{
	//Creating a String object
	String str1= "Always think positive!";


	//Printing the original String
	Console.WriteLine("Original String is:"+ str1);


	//Calling the Remove() method on the String object
	//to remove all characters starting from index 2 and has a length of 5 characters
	Console.WriteLine("After removing characters from index 2 and a length of 5:"+ str1.Remove(2,5));


	//Calling the Remove() method on the String object
	//to remove all characters starting from index 6 and has a length of 2 characters
	Console.WriteLine("After removing characters from index 6 and a length of 2:"+ str1.Remove(6,2));


	//Calling the Remove() method on the String object
	//to remove all characters starting from index 0 and has a length of 5 characters
	Console.WriteLine("After removing characters from index 0 and a length of 5:"+ str1.Remove(0,5) );
	

	//Calling the Remove() method on the String object
	//to remove all characters starting from index 4 and has a length of 4 characters
	Console.WriteLine("After removing characters from index 4 and a length of 4:"+ str1.Remove(4,4) );
}
}


Output is :


Original String is:Always think positive!
After removing characters from index 2 and a length of 5:Althink positive!
After removing characters from index 6 and a length of 2:Alwayshink positive!
After removing characters from index 0 and a length of 5:s think positive!
After removing characters from index 4 and a length of 4:Alwahink positive!





An ArgumentOutOfRangeException thrown while calling the Remove() method


You have to be careful when passing an index to any version of the Remove() method, because passing an out-of-range index to it will throw an exception of type ArgumentOutOfRangeException, let us show you how.

//C# Example of Remove(int startIndex, int length)


using System;

class StringRemove
{
public static void Main()
{
	//Creating a String object
	String str1= "Staying Alive!";


	//Printing the original String
	Console.WriteLine("Original String is:"+ str1);


	//Calling the Remove() method on the String object
	//to remove all characters starting from index 2 and has a length of 5 characters
	Console.WriteLine("After removing characters from index 2 and a length of 5:"+ str1.Remove(2,5));


	//Calling the Remove() method on the String object
	//to remove all characters starting from index 6
	Console.WriteLine("After removing characters from index 6 and a length of 2:"+ str1.Remove(6));


	//Calling the Remove() method with an out-of-range index
	Console.WriteLine("After removing characters from index 0 and a length of 5:"+ str1.Remove(20) );
}
}


Output is :


Original String is:Staying Alive!
After removing characters from index 2 and a length of 5:St Alive!
After removing characters from index 6 and a length of 2:Stayin

Unhandled Exception: System.ArgumentOutOfRangeException: startIndex must be less than length of string.
Parameter name: startIndex
   at System.String.Remove(Int32 startIndex)
   at StringRemoveEx.Main()





The Remove() method does not modify the original content in a String object


Although we have already explained how String objects are immutable i.e. they cannot be modified but still for those who would like it to proved to them. Let us show you an example to display how either version of the Remove() only returns a modified copy from the invoked String object i.e. it does not modify the original content of a String object. Let's understand this by an example.

//C# The Remove() method doesn't modify the String object(it actually can't because String objects are immutable)

using System;

class StringRemove
{
public static void Main()
{
	//Creating a String object
	String str1= "Always look at the bright side of life!";


	//Printing the original String
	Console.WriteLine("String before calling the Remove(): "+ str1);


	//Calling the Remove() method on the String object
	//to remove all characters starting from index 2 and has a length of 5 characters
	Console.WriteLine("After removing characters from index 2 and a length of 5:"+ str1.Remove(2,5));


	//Calling the Remove() method on the String object
	//to remove all characters starting from index 6
	Console.WriteLine("After removing characters from index 6 and a length of 2:"+ str1.Remove(6));


	//Calling the Remove() method with an out-of-range index
	Console.WriteLine("After removing characters from index 0 and a length of 5:"+ str1.Remove(20));	
	

	//Printing the String after calling the Remove() method
	Console.WriteLine("String after calling the Remove(): "+ str1);

}
}


Output is :


String before calling the Remove(): Always look at the bright side of life!
After removing characters from index 2 and a length of 5:Allook at the bright side of life!
After removing characters from index 6 and a length of 2:Always
After removing characters from index 0 and a length of 5:Always look at the b
String after calling the Remove(): Always look at the bright side of life!





Please share this article -





< Prev
Next >
< C# Split() Method
C# Contains() Method >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement