Java StringBuilder substring() Method
Example
Return the text from position 6 to the end:
StringBuilder sb = new StringBuilder("Hello World");
String result = sb.substring(6);
System.out.println(result);
Definition and Usage
The substring() method returns the characters between two positions as a new String.
If you leave out end, it returns the characters to the end of the text. The StringBuilder itself does not change.
Syntax
One of the following:
public String substring(int start)
public String substring(int start, int end)
Parameter Values
| Parameter | Description |
|---|---|
| start | Required. The index of the first character. |
| end | Optional. The index after the last character. |
Technical Details
| Returns: | A String with the characters between the two positions. |
|---|---|
| Throws: | StringIndexOutOfBoundsException - if start or end is negative, end is larger than the length, or start is larger than end. |
Related Pages
Java String substring() Method
❮ StringBuilder Methods