Java StringBuilder subSequence() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.StringBuilder

Definition

In Java, sb.subSequence(start, end) returns a CharSequence containing the characters from index start (inclusive) to end (exclusive) without modifying the StringBuilder.

StringBuilder implements the CharSequence interface (alongside String and CharBuffer), so subSequence() lets you pass a slice directly to methods that accept any CharSequence.

An instance method of the java.lang.StringBuilder class (since Java 1.5, JDK 1.5): a final, mutable sequence of UTF-16 characters that is not thread-safe. Call it on an existing builder, as in sb.subSequence(...). No import is needed because java.lang is always visible.

Syntax

Pass both inclusive start and exclusive end indexes.

Java
StringBuilder sb = new StringBuilder("Hello, Java!");
CharSequence seq = sb.subSequence(7, 11);   // "Java"
CallWhat it does
sb.subSequence(0, 4)Returns a CharSequence holding indexes 0..3.
dest.append(sb.subSequence(0, 4))Passes a slice straight into another builder's append(CharSequence).
word.contentEquals(sb.subSequence(2, 6))Compares a String directly against a builder slice.

In OpenJDK, StringBuilder.subSequence(start, end) delegates to substring(start, end), though its declared return type is CharSequence rather than String.

Library declarations
Java
// java.lang.StringBuilder
public CharSequence subSequence(int start, int end)

Parameters

ParameterDescription
start (int)Inclusive zero-based start index (0 <= start <= end).
end (int)Exclusive end index (start <= end <= length()).

Return Value

Return ValueDescription
CharSequenceA CharSequence holding the characters in [start, end).

Compare with Similar Methods

Slicing methods on StringBuilder:

MethodHow it differs
substring(start, end)Returns String directly and also provides a single-argument substring(start).
getChars(...)Copies a slice into a caller-owned char[].
String.contentEquals(cs)Accepts any CharSequence such as a subSequence result.
append(cs, start, end)Appends a subrange of another CharSequence without creating an intermediate slice.

Java Examples

Example 1 — Pass subSequence() to append() and contentEquals()

Feed a CharSequence slice directly into another StringBuilder and String.contentEquals().

Java
public class StringBuilderSubSequenceApi {
    public static void main(String[] args) {
        StringBuilder source = new StringBuilder("User:Alice:Admin");
        CharSequence name = source.subSequence(5, 10);

        StringBuilder greeting = new StringBuilder("Hello, ");
        greeting.append(name).append('!');

        System.out.println(greeting);
        System.out.println("Matches Alice? " + "Alice".contentEquals(name));
    }
}

How It Works

  1. Slice as CharSequence. subSequence(5, 10) pulls "Alice" without modifying source.
  2. Works directly in append(). Because append has a CharSequence overload, no toString() conversion is needed.
  3. Compare without casting. "Alice".contentEquals(name) checks the characters of the CharSequence cleanly.

Example 2 — Compare subSequence() With substring()

Check length, characters, and content equality between subSequence() and substring().

Java
public class StringBuilderSubSequenceVsSub {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("JavaBuilder");
        CharSequence seq = sb.subSequence(4, sb.length());
        String str = sb.substring(4, sb.length());

        System.out.println("seq length: " + seq.length() + ", charAt(0): " + seq.charAt(0));
        System.out.println("Equal content? " + str.contentEquals(seq));
    }
}

How It Works

  1. Same slice bounds. Both calls span indexes 4 through 10 ("Builder").
  2. CharSequence contract. Using seq.length() and seq.charAt(0) relies only on CharSequence methods.
  3. Identical characters. str.contentEquals(seq) returns true because both carry the same text.

Example 3 — Handle Empty Ranges (start == end) and Bounds Errors

Check that start == end produces an empty sequence, while reversed bounds throw IndexOutOfBoundsException.

Java
public class StringBuilderSubSequenceBounds {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder("ABCD");
        CharSequence empty = sb.subSequence(2, 2);
        System.out.println("Empty slice length = " + empty.length());

        try {
            sb.subSequence(3, 1);
        } catch (IndexOutOfBoundsException e) {
            System.out.println("Caught invalid subSequence bounds");
        }
    }
}

How It Works

  1. Zero-length slice valid. When start == end, zero characters lie in the half-open interval [2, 2).
  2. Invalid order caught. Passing start > end (3 > 1) throws IndexOutOfBoundsException.
  3. Original unaffected. Calling subSequence never shrinks or changes sb.

Frequently Asked Questions

StringBuilder implements the CharSequence interface, which requires subSequence(int start, int end). substring() returns a String directly and also offers a one-argument overload.
No. It leaves the StringBuilder unchanged.
In standard OpenJDK implementations it delegates to substring() and returns an independent String, though the method signature only guarantees a CharSequence with the sliced characters.
No. subSequence always requires both start and end indexes; use substring(start) for one argument.
It throws IndexOutOfBoundsException (specifically StringIndexOutOfBoundsException in current JDKs).

Next: substring()

Extract part or all of the builder as a brand-new immutable String.

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.

9 people found this page helpful