Java String subSequence() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, subSequence(beginIndex, endIndex) returns the characters from beginIndex up to (not including) endIndex, declared with the interface type CharSequence instead of String.

It exists because String implements the CharSequence interface, which requires a subSequence method. The documentation says it behaves exactly like substring(beginIndex, endIndex), so the object you get back is in fact a String.

subSequence() is an instance method of java.lang.String, added in Java 1.4. Call it on a string, as in text.subSequence(...); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result.

Syntax

Pass a start and an end index. Store the result as CharSequence, or call toString() when you need a String variable.

java
CharSequence part = word.subSequence(0, 7);
String asText     = part.toString();
int size          = part.length();
CallWhat it does
"Java".subSequence(1, 3)The CharSequence "av".
"Java".subSequence(0, 4)All four characters.
"Java".subSequence(2, 2)An empty sequence.
"Java".subSequence(3, 1)Throws an IndexOutOfBoundsException.

Most code simply calls substring. subSequence is useful when you write methods that accept any CharSequence (a String, a StringBuilder, and so on) and want to pass a slice without caring about the concrete type.

Library declarations
java
public CharSequence subSequence(int beginIndex, int endIndex);

Parameters

ParameterDescription
beginIndexFirst index to include.
endIndexIndex to stop before (exclusive).

Return Value

Return ValueDescription
CharSequenceThe requested characters. For String the object is a new String; the original is unchanged.

Java Examples

Example 1 — Work with the Returned CharSequence

Slice "Programming" and use the methods every CharSequence offers.

java
public class SubSequenceBasic {
    public static void main(String[] args) {
        String word = "Programming";
        CharSequence part = word.subSequence(0, 7);

        System.out.println("part:   " + part);
        System.out.println("length: " + part.length());
        System.out.println("char 3: " + part.charAt(3));
        System.out.println("String? " + (part instanceof String));
    }
}

How It Works

  1. Seven characters. Indexes 0 to 6 of "Programming" spell "Program"; index 7 (the second 'm') is excluded.
  2. Interface methods only. Through a CharSequence variable you can call length(), charAt(), and toString(); charAt(3) is 'g'.
  3. Really a String. The instanceof test prints true, because the call delegates to substring.

Example 2 — Pass a Slice to a CharSequence Method

A helper that accepts any CharSequence can receive a whole string or just a part of it.

java
public class SubSequenceVowels {
    static int countVowels(CharSequence cs) {
        int count = 0;
        for (int i = 0; i < cs.length(); i++) {
            if ("aeiouAEIOU".indexOf(cs.charAt(i)) >= 0) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        String sentence = "Hello World";
        System.out.println(countVowels(sentence.subSequence(0, 5)));
        System.out.println(countVowels(sentence.subSequence(6, 11)));
        System.out.println(countVowels(sentence));
    }
}

How It Works

  1. One parameter type for all. countVowels(CharSequence cs) accepts the full sentence and both slices without overloads.
  2. Count per word. "Hello" (indexes 0-4) holds e and o, so 2; "World" (6-10) holds only o, so 1.
  3. Whole text. Passing the String itself counts all three vowels; the slices did not alter it.

Example 3 — Compare with substring() and Catch a Bad Range

Check that both methods agree, then see the exception for reversed indexes.

java
public class SubSequenceCompare {
    public static void main(String[] args) {
        String code = "ABC-123";
        CharSequence seq = code.subSequence(4, 7);
        String sub = code.substring(4, 7);

        System.out.println(seq.equals(sub));
        System.out.println("123".contentEquals(seq));
        System.out.println(seq.toString().length());

        try {
            code.subSequence(5, 2);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Bad range: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. Same characters. Both calls pick indexes 4-6, the digits "123", so seq.equals(sub) is true.
  2. Compare by content. contentEquals accepts any CharSequence and confirms the text is "123".
  3. Reversed indexes fail. subSequence(5, 2) throws; catching the documented IndexOutOfBoundsException also catches the StringIndexOutOfBoundsException OpenJDK actually throws.

Frequently Asked Questions

Only the declared return type. subSequence() returns CharSequence, substring() returns String. For a String both give the same characters.
String implements the CharSequence interface, and that interface requires subSequence(). It lets generic code slice any character sequence.
Call toString() on it, for example String s = text.subSequence(0, 3).toString();
Yes. subSequence(1, 3) returns the characters at indexes 1 and 2, exactly like substring(1, 3).
IndexOutOfBoundsException (StringIndexOutOfBoundsException in practice) when an index is negative, endIndex is larger than length(), or beginIndex is larger than endIndex.

Next: substring()

Get a slice of a string by start and end index.

substring() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful