The delete(int start, int end) method of StringBuilder class removes the characters starting from index start to index end-1 from String contained by StringBuilder. This method takes two indexes as a parameter first start represents index of the first character and endIndex represents index after the last character of the substring to be removed from String contained by StringBuilder and returns the remaining String as StringBuilder Object.
Syntax:
public StringBuilder delete(int start, int end)
Parameters: This method accepts two parameters:
- start: index of the first character of the substring.
- end: index after the last character of the substring.
Return Value: This method returns this StringBuilder object after removing the substring.
Exception: This method throws StringIndexOutOfBoundsException if the start is less than zero, or start is larger than the length of String, or start is larger than end.
Below programs demonstrate the delete() method of StringBuilder Class:
Example 1:
// Java program to demonstrate // the delete() Method. class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder( "WelcomeGeeks" );
// print string
System.out.println( "Before removal String = "
+ str.toString());
// remove the substring from index 2 to 8
StringBuilder afterRemoval = str.delete( 2 , 8 );
// print string after removal of substring
System.out.println( "After removal String = "
+ afterRemoval.toString());
}
} |
Before removal String = WelcomeGeeks After removal String = Weeeks
Example 2:
// Java program to demonstrate // the delete() Method. class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder( "GeeksforGeeks" );
// print string
System.out.println( "Before removal String = "
+ str.toString());
// remove the substring from index 8 to 8
StringBuilder afterRemoval = str.delete( 8 , 8 );
// start equal to end so no change in string
// print string after removal of substring
System.out.println( "After removal of SubString"
+ " start=8 to end=8"
+ " String becomes => "
+ afterRemoval.toString());
// remove the substring from index 1 to 8
afterRemoval = str.delete( 1 , 8 );
// print string after removal of substring
System.out.println( "After removal of SubString"
+ " start=1 to end=8"
+ " String becomes => "
+ afterRemoval.toString());
}
} |
Before removal String = GeeksforGeeks After removal of SubString start=8 to end=8 String becomes => GeeksforGeeks After removal of SubString start=1 to end=8 String becomes => GGeeks
Example 3: To demonstrate IndexOutOfBoundException
// Java program to demonstrate // exception thrown by the delete() Method. class GFG {
public static void main(String[] args)
{
// create a StringBuilder object
// with a String pass as parameter
StringBuilder
str
= new StringBuilder( "GeeksforGeeks" );
try {
// make start greater than end
StringBuilder afterRemoval = str.delete( 7 , 4 );
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
} |
Exception: java.lang.StringIndexOutOfBoundsException
Reference:
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuilder.html#delete(int, int)