Java StringBuilder delete() Method
Example
Remove " World" from a StringBuilder:
StringBuilder sb = new StringBuilder("Hello World");
sb.delete(5, 11);
System.out.println(sb);
Definition and Usage
The delete() method removes the characters between two positions.
The character at end is not removed. If end is larger than the length, the characters are removed to the end of the text.
Tip: Use deleteCharAt() to remove a single character.
Syntax
public StringBuilder delete(int start, int end)
Parameter Values
| Parameter | Description |
|---|---|
| start | Required. The index of the first character to remove. |
| end | Required. The index after the last character to remove. |
Technical Details
| Returns: | The same StringBuilder, with the characters removed. |
|---|---|
| Throws: | StringIndexOutOfBoundsException - if start is negative, larger than the length, or larger than end. |
Related Pages
❮ StringBuilder Methods