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


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)


  • 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.


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.


 * A java example source code to demonstrate
 * the use of subSequence() method of StringBuilder class

public class StringBuilderSubSequenceExample {

	public static void main(String[] args) {

		// initialize the StringBuilder object
		StringBuilder sb = new StringBuilder("");
		System.out.println("Contents of buffer:" + sb);

		 * get the CharSequence from this StringBuilder
		 * starting from 
		 * index 3 to index 10		 
		int start = 3;
		int end = 10;
		CharSequence cs = sb.subSequence(start, end);
		System.out.println("Results:" + cs);

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 “” 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