![]() |
VOOZH | about |
The delete(int start, int end) method of the StringBuilder class removes a sequence of characters from the string represented by the StringBuilder object. Characters are deleted starting from index start (inclusive) up to end - 1 (exclusive).
public StringBuilder delete(int start, int end)
Parameters: This method accepts two parameters:
Return Value: This method returns this StringBuilder object after removing the substring.
Before removal String = WelcomeGeeks After removal String = Weeeks
Explanation: Characters from index 2 to 7 ("lcomeG") are removed, and the remaining string is returned.
Before removal String = GeeksforGeeks After delete(8, 8) => GeeksforGeeks After delete(1, 8) => GGeeks
Explanation: delete(8, 8) removes nothing because the range is empty while delete(1, 8) removes characters from index 1 to 7
Exception: java.lang.StringIndexOutOfBoundsException: Range [7, 4) out of bounds for length 13
Explanation: An exception is thrown because start is greater than end, which is an invalid range.
Method | Purpose |
|---|---|
delete(start, end) | Removes a range of characters |
deleteCharAt(index) | Removes a single character |
substring(start, end) | Returns a new string without modifying original |