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

Syntax:

public CharSequence subSequence(int start,int end)

Java Code Example :

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

This method will result to the same value as that of substring(int start, int end) its just that in this case the result is a CharSequence object.

Make a note that IndexOutOfBoundsException will be thrown if start or end are negative or greater than length(), or start is greater than end.

package com.javatutorialhq.java.examples;

/*
 * This example source code demonstrates the use of 
 * subSequence(int start,int end) method of StringBuffer class
 */

public class StringBufferSubSequence {

	public static void main(String[] args) {

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

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

	}
}

Sample Output :

Running the above example source code will give the following output

subSequence(int start,int end) method example

subSequence(int start,int end) method example

Suggested Reading List :

References :