The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point.
Syntax :
public StringBuffer delete(int start_point, int end_point)
Parameters : The method accepts two parameters of integer type:
start_point – This refers to the beginning index and is included in the count.
end_point – This refer to the ending index and is excluded from the count.
Return Value : The method returns the string after deleting the substring formed by the range mentioned in the parameters.
Exceptions : StringIndexOutOfBoundsException occurs if the start_point is negative, greater than length(), or greater than the end_point.
Examples :
Input: String = "Apple"
start_point = 2
end_point = 4
Output: Ape
Input: String = "GeeksforGeeks"
start_point = 2
end_point = 7
Output: GerGeeks
Below programs illustrate the java.lang.StringBuffer.delete() method:
Program 1:
// Java praogram to illustrate the // java.lang.StringBuffer.delete() import java.lang.*; public class geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Geeksforgeeks"); System.out.println("string buffer = " + sbf); // Deleting characters from index 2 to 7 sbf.delete(6, 8); System.out.println("After deletion string buffer is = " + sbf); } } |
string buffer = Geeksforgeeks After deletion string buffer is = Geeksfgeeks
Program 2:
// Java praogram to illustrate the // java.lang.StringBuffer.delete() import java.lang.*; public class geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks"); System.out.println("string buffer = " + sbf); // deleting characters from index 5 to index 9 sbf.delete(5, 9); System.out.println("After deletion string buffer is = " + sbf); } } |
string buffer = Welcome to Geeksforgeeks After deletion string buffer is = Welcoo Geeksforgeeks
Program 3: Here the index is negative.
// Java praogram to illustrate the // java.lang.StringBuffer.delete() import java.lang.*; public class geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks"); System.out.println("string buffer = " + sbf); sbf.delete(-5, 9); System.out.println("After deletion string buffer is = " + sbf); } } |
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: -5
at java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:756)
at java.lang.StringBuffer.delete(StringBuffer.java:430)
at geeks.main(geeks.java:13)
Program 4: Here the index is not present.
// Java praogram to illustrate the // java.lang.StringBuffer.delete() import java.lang.*; public class geeks { public static void main(String[] args) { StringBuffer sbf = new StringBuffer("Welcome to Geeksforgeeks"); System.out.println("string buffer = " + sbf); sbf.delete(99, 109); System.out.println("After deletion string buffer is = " + sbf); } } |
Exception in thread "main" java.lang.StringIndexOutOfBoundsException
at java.lang.AbstractStringBuilder.delete(AbstractStringBuilder.java:760)
at java.lang.StringBuffer.delete(StringBuffer.java:430)
at geeks.main(geeks.java:13)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer appendCodePoint() Method in Java with Examples
- StringBuffer replace() Method in Java with Examples
- StringBuffer reverse() Method in Java with Examples
- StringBuffer append() Method in Java with Examples
- StringBuffer codePointCount() method in Java with Examples
- StringBuffer codePointBefore() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
- StringBuffer toString() method in Java with Examples
- StringBuffer codePointAt() method in Java with Examples
- StringBuffer ensureCapacity() method in Java with Examples
- StringBuffer offsetByCodePoints() method in Java with Examples
- StringBuffer setCharAt() method in Java with Examples
- StringBuffer getChars() method in Java with Examples
- StringBuffer indexOf() method in Java with Examples
- StringBuffer substring() method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
- Matcher appendTail(StringBuffer) method in Java with Examples
- Matcher appendReplacement(StringBuffer, String) method in Java with Examples
- StringBuffer setLength() in Java with Examples
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.

