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();
Call
What 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
Parameter
Description
beginIndex
First index to include.
endIndex
Index to stop before (exclusive).
Return Value
Return Value
Description
CharSequence
The 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));
}
}
Output
part: Program
length: 7
char 3: g
String? true
How It Works
Seven characters. Indexes 0 to 6 of "Programming" spell "Program"; index 7 (the second 'm') is excluded.
Interface methods only. Through a CharSequence variable you can call length(), charAt(), and toString(); charAt(3) is 'g'.
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));
}
}
Output
2
1
3
How It Works
One parameter type for all.countVowels(CharSequence cs) accepts the full sentence and both slices without overloads.
Count per word."Hello" (indexes 0-4) holds e and o, so 2; "World" (6-10) holds only o, so 1.
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.
true
true
3
Bad range: StringIndexOutOfBoundsException
How It Works
Same characters. Both calls pick indexes 4-6, the digits "123", so seq.equals(sub) is true.
Compare by content.contentEquals accepts any CharSequence and confirms the text is "123".
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.