![]() |
VOOZH | about |
In Java, the StringBuffer class is used to create mutable sequences of characters. It allows modification of strings without creating new objects. One of the important methods provided by the StringBuffer class is the delete() method, which is used to remove a portion of characters from the string.
Hello
Explanation: Original string: "HelloWorld", Characters from index 5 to 9 are removed. Remaining string becomes "Hello"
public StringBuffer delete(int start, int end)
Parameters:
Return Value: Returns the same StringBuffer object after removing the specified substring.
Deletes characters between index 5 and 9.
StringBuffer = Welcome to Geeksforgeeks After deletion = Welcoo Geeksforgeeks
Passing a negative index causes an exception.
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)
Using indexes outside the valid range throws an exception.
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)