Java String subSequence() Method with Examples
In Java, the String
class provides the subSequence()
method. This subSequence()
method extracts a portion of a string as a CharSequence
. This method is useful for retrieving specific character ranges from a string which enhances string manipulation.
In this article, we will learn how to use the subSequence()
method in Java with practical examples.
Example:
Below is the basic example to demonstrate the working of the String.subSequence() function.
// Java Program to demonstrate the working of
// String.subSequence() method
import java.lang.Math;
class SubSequence {
public static void main(String args[]) {
String s = "Programming";
// Extracting substring from index 7 to 11 (exclusive)
CharSequence sub = s.subSequence(7, 11);
System.out.println("" + sub);
}
}
// Java Program to demonstrate the working of
// String.subSequence() method
import java.lang.Math;
class SubSequence {
public static void main(String args[]) {
String s = "Programming";
// Extracting substring from index 7 to 11 (exclusive)
CharSequence sub = s.subSequence(7, 11);
System.out.println("" + sub);
}
}
Output
ming
Table of Content
Syntax of subSequence() Method
CharSequence subSequence(int start, int end);
Parameters:
- start: This is the index from where the subsequence starts, it is inclusive.
- end: This is the index where the subsequence ends, it is exclusive.
Returns Value: It returns the specified subsequence in range [start, end).
Errors and Exceptions: IndexOutOfBoundsException: It throws this error if start or end are negative, if end is greater than length(), or if start is greater than end.
Other Ways to Use Java String.subSequence() Method
Here, we will learn some other practical examples demonstrating the usage of the Java String.subSequence() method.
1. Using subSequence() with Different Indices
This extracts a portion of the string by specifying a custom starting and ending index. It excludes the character at the end index.
Example:
// Java Program to demonstrate the usage of
// subSequence() method with different indices
public class SubSequence {
public static void main(String[] args) {
String s = "Programming";
// Extracting substring from index 3 to 6 (exclusive)
CharSequence sub = s.subSequence(3, 7);
System.out.println("" + sub);
}
}
// Java Program to demonstrate the usage of
// subSequence() method with different indices
public class SubSequence {
public static void main(String[] args) {
String s = "Programming";
// Extracting substring from index 3 to 6 (exclusive)
CharSequence sub = s.subSequence(3, 7);
System.out.println("" + sub);
}
}
Output
gram
Explanation: The subSequence(3, 7)
extracts characters starting from index 3 and ending at index 6 (index 7 is exclusive).
2. Using subSequence()
with a Full Range
This extracts a sequence of characters from the entire string by providing indices from 0 to the string length.
Example:
// Java Program to demonstrate extracting the full string
// using subSequence() method
public class SubSequence {
public static void main(String[] args) {
String s = "Programming";
// Extracting the full string
CharSequence sub = s.subSequence(0, s.length());
System.out.println("" + sub);
}
}
// Java Program to demonstrate extracting the full string
// using subSequence() method
public class SubSequence {
public static void main(String[] args) {
String s = "Programming";
// Extracting the full string
CharSequence sub = s.subSequence(0, s.length());
System.out.println("" + sub);
}
}
Output
Programming
3. Handling Index Out of Bounds
When the provided indices are invalid, for example, start is greater than end or indices are out of range, it throws a StringIndexOutOfBoundsException
.
Example:
public class SubSequence {
public static void main(String[] args) {
String s = "Programming";
try {
// Extracting using an invalid range
CharSequence sub = s.subSequence(2, 20);
System.out.println("" + sub);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e);
}
}
}
public class SubSequence {
public static void main(String[] args) {
String s = "Programming";
try {
// Extracting using an invalid range
CharSequence sub = s.subSequence(2, 20);
System.out.println("" + sub);
} catch (IndexOutOfBoundsException e) {
System.out.println("Error: " + e);
}
}
}
Output
Error: java.lang.StringIndexOutOfBoundsException: begin 2, end 20, length 11
Explanation:
- The above example shows, how the
subSequence()
method throws an exception if theend
index exceeds the string length. - It catches and displays the
IndexOutOfBoundsException
.