Java StringBuildersubSequence Examples

In this tutorial, we will learn about the Java StringBuilder.subSequence() function, and learn how to use this function to find the subsequence of this StringBuilder sequence, with the help of examples.

subSequenceint start, int end

StringBuilder.subSequence() returns a new character sequence that is a subsequence of this sequence whose start and end are specified as arguments.

Syntax

The syntax of subSequence() function is

subSequence(int start, int end)

where

Parameter Description
start The index at which subsequence starts in StringBuilder.
end The index at which subsequence ends in StringBuilder.

Returns

The function returns CharSequence object.

Example 1 subSequencestart, end

In this example, we will take a StringBuilder initialized with a sequence "abcdefghijklm", and we shall find the subsequence that starts at index 3 and ends at index 7.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklm");
        System.out.println("Original sequence : " + stringBuilder.toString());
        
        int start = 3;
        int end = 7;
        CharSequence str = stringBuilder.subSequence(start, end);
        System.out.println("Sub-sequence      : " + str);
    }
}

Output

Original sequence : abcdefghijklm
Sub-sequence      : defg

Example 2 subSequencestart, end end length

In this example, we will take a StringBuilder initialized with a sequence "abcdefghijklm", and we shall find the subsequence that starts at index 3 and ends at index 15. Since, the end is greater than the length of this sequence, subSequence() throws java.lang.StringIndexOutOfBoundsException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklm");
        System.out.println("Original sequence : " + stringBuilder.toString());
        
        int start = 3;
        int end = 15;
        CharSequence str = stringBuilder.subSequence(start, end);
        System.out.println("Sub-sequence      : " + str);
    }
}

Output

Original sequence : abcdefghijklm
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start 3, end 15, length 13
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.substring(Unknown Source)
	at java.base/java.lang.StringBuilder.substring(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.subSequence(Unknown Source)
	at java.base/java.lang.StringBuilder.subSequence(Unknown Source)
	at Example.main(Example.java:8)

Example 3 subSequencestart, end start end

In this example, we will take a StringBuilder initialized with a sequence "abcdefghijklm", and we shall find the subsequence that starts at index 7 and ends at index 3. Since, the end is greater than the length of this sequence, subSequence() throws java.lang.StringIndexOutOfBoundsException.

Java Program

public class Example { 
    public static void main(String[] args) { 
        StringBuilder stringBuilder = new StringBuilder("abcdefghijklm");
        System.out.println("Original sequence : " + stringBuilder.toString());
        
        int start = 7;
        int end = 3;
        CharSequence str = stringBuilder.subSequence(start, end);
        System.out.println("Sub-sequence      : " + str);
    }
}

Output

Original sequence : abcdefghijklm
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: start 7, end 3, length 13
	at java.base/java.lang.AbstractStringBuilder.checkRangeSIOOBE(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.substring(Unknown Source)
	at java.base/java.lang.StringBuilder.substring(Unknown Source)
	at java.base/java.lang.AbstractStringBuilder.subSequence(Unknown Source)
	at java.base/java.lang.StringBuilder.subSequence(Unknown Source)
	at Example.main(Example.java:8)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java StringBuilder.subSequence() function, and also learnt how to use this function with the help of examples.