Advertisement
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. |
//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);
}
}
Original String is 0123456789
String returned after removing characters from index 3 to its end: 012
Advertisement
//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) );
}
}
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!
//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) );
}
}
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()
//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);
}
}
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!
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement