Java StringBuilder subSequence() Method
Example
Return the first 5 characters:
StringBuilder sb = new StringBuilder("Hello World");
System.out.println(sb.subSequence(0, 5));
Definition and Usage
The subSequence() method returns the characters between two positions as a CharSequence.
It works like substring(), but returns a CharSequence instead of a String. In most cases, substring() is easier to use.
Syntax
public CharSequence subSequence(int start, int end)
Parameter Values
| Parameter | Description |
|---|---|
| start | Required. The index of the first character. |
| end | Required. The index after the last character. |
Technical Details
| Returns: | A CharSequence with the characters between the two positions. |
|---|---|
| Throws: | IndexOutOfBoundsException - if start or end is negative, end is larger than the length, or start is larger than end. |
Related Pages
Java String subSequence() Method
❮ StringBuilder Methods