Java.lang.StringBuilder.delete() Method
Description
The java.lang.StringBuilder.delete() method removes the characters in a substring of this sequence.
The substring begins at the specified start and extends to the character at index end - 1 or to the end of the sequence if no such character exists. If start is equal to end, no changes are made.
Declaration
Following is the declaration for java.lang.StringBuilder.delete() method
public StringBuilder delete(int start, int end)
Parameters
start -- This is the beginning index, inclusive.
end -- This is the ending index, exclusive.
Return Value
This method returns this object.
Exception
StringIndexOutOfBoundsException -- if start is negative, greater than length(), or greater than end.
Example
The following example shows the usage of java.lang.StringBuilder.delete() method.
package com.tutorialspoint;
import java.lang.*;
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("Java lang package");
System.out.println("string = " + str);
// deleting characters from index 4 to index 9
str.delete(4, 9);
System.out.println("After deletion = " + str);
}
}
Let us compile and run the above program, this will produce the following result:
string = Java lang package After deletion = Java package