java.lang.StringBuilder subSequence(int start,int end)

Description

On this document we will be showing a java example on how to use the subSequence(int start,int end) method of StringBuilder Class. Basically the subsequence() method returns a new character sequence that is a subsequence of this sequence. Below is the circumstance where the subsequence() method will yield the same as that of substring.

sb.subsequence(start, end)

is equivalent to

sb.substring(start, end)

Notes:

  • An IndexOutOfBoundsException will be thrown by the subsequence() method if end is greater than length(), or if start is greater than end.
  • This method is specified by subSequence in interface CharSequence.

Method Syntax

public CharSequence subSequence(int start,int end)

Method Argument

Data Type Parameter Description
int start the start index, inclusive.
int end the end index, exclusive.

Method Returns

The subSequence(int start,int end) method returns the specified subsequence.

Compatibility

Requires Java 1.5 and up

Java StringBuilder subSequence(int start,int end) Example

Below is a java code demonstrates the use of subSequence(int start,int end) method of StringBuilder class. The example presented might be simple however it shows the behavior of the subSequence(int start,int end) method.

The java example source code above, demonstrates the use of subSequence(int start,int end) method of StringBuilder class. Initially the code assigns a string “javatutorialhq.com” as initial contents of the StringBuilder. Then we use the subsequence to get a part of the sequence based from index 3 until the end index 10.

Sample Output

Below is the sample output when you run the above example.

StringBuilder subSequence() example output