The subSequence(int start, int end) method of StringBuffer class is the inbuilt method used to return a subsequence of characters lie between index start and end-1 of this sequence. The subsequence starts with the char value at the index start and ends with the char value at (end-1). The length of the returned subsequence is end-start. So if start is equal to end then an empty subsequence is returned.
Syntax:Â Â
public CharSequence subSequence?(int start, int end)
Parameters:Â
This method accepts two parameters:
- start which is Integer type value refers to the start index of subsequence.
- end which is Integer type value refers to the last index of subsequence.
Returns:Â
This method returns the specified subsequence in range start to end-1.Â
Exception:Â
if start or end are negative, if end is greater than length(), or if start is greater than end then IndexOutOfBoundsException is thrown.
Below programs illustrate the java.lang.StringBuffer.subSequence() method:Â
Example 1:Â
Java
// Java program to demonstrate// the subSequence() Method.Â
class GFG {Â
    public static void main(String[] args)    {        // create a StringBuffer object        // with a String pass as parameter        StringBuffer str            = new StringBuffer("WelcomeGeeks");Â
        // print string        System.out.println("String contains = " + str);Â
        // get subSequence between index 0 to 7        // using subSequence() and print        System.out.println("SubSequence = "                           + str.subSequence(0, 7));    }} |
Output:Â
String length = 12 and contains = WelcomeGeeks SubSequence = Welcome
Example 2:Â
Java
// Java program to demonstrate// the subSequence() Method.Â
class GFG {Â
    public static void main(String[] args)    {        // create a StringBuffer object        // with a String pass as parameter        StringBuffer str            = new StringBuffer("Indian Team Played Well");Â
        // print string        System.out.println("String contains = " + str);Â
        // get subSequence between index 0 to 7        // using subSequence() and print        System.out.println("SubSequence = "                           + str.subSequence(7, 18));    }} |
Output:Â
String contains = Indian Team Played Well SubSequence = Team Played
Example 3: When start > end:
Java
// Java program to demonstrate// Exception thrown by the subSequence() Method.Â
class GFG {Â
    public static void main(String[] args)    {        // create a StringBuffer object        // with a String pass as parameter        StringBuffer str            = new StringBuffer("Indian Team Played Well");Â
        try {            // get subSequence between index 0 to 7            // using subSequence() and print            System.out.println(str.subSequence(19, 18));        }        catch (Exception e) {            e.printStackTrace();        }    }} |
Output:Â
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.AbstractStringBuffer.substring(AbstractStringBuffer.java:935)
at java.lang.StringBuffer.substring(StringBuffer.java:76)
at java.lang.AbstractStringBuffer.subSequence(AbstractStringBuffer.java:912)
at java.lang.StringBuffer.subSequence(StringBuffer.java:76)
at GFG.main(File.java:16)
References:Â
https://docs.oracle.com/javase/10/docs/api/java/lang/StringBuffer.html#subSequence(int, int)
