Java String subSequence() Method with Examples
Last Updated :
21 Nov, 2024
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
// 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);
}
}
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
// 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);
}
}
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
// 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);
}
}
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:
Java
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);
}
}
}
OutputError: java.lang.StringIndexOutOfBoundsException: begin 2, end 20, length 11
Explanation:
- The above example shows, how the
subSequence()
method throws an exception if the end
index exceeds the string length. - It catches and displays the
IndexOutOfBoundsException
.
Similar Reads
StringBuffer subSequence() in Java with Examples The subSequence(int start, int end) method of StringBuffer class is the inbuilt method used to return a subsequence of characters lie between index start and end-1 of this sequence. The subsequence starts with the char value at the index start and ends with the char value at (end-1). The length of t
2 min read
StringBuilder subSequence() in Java with Examples The subSequence(int start, int end) method of StringBuilder class is the inbuilt method used to return a subsequence of characters lie between index start and end-1 of this sequence. The subsequence starts with the char value at the index start and ends with the char value at (end-1). The length of
2 min read
StringBuffer substring() method in Java with Examples In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start) The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The strin
4 min read
CharBuffer subSequence() methods in Java with Examples The subSequence() method of java.nio.CharBuffer Class is used to create a new character buffer that represents the specified subsequence of this buffer, relative to the current position. The new buffer will share this buffer's content; that is, if the content of this buffer is mutable then modificat
3 min read
StringBuffer delete() Method in Java with Examples The java.lang.StringBuffer.delete() is an inbuilt method in Java which is used to remove or delete the characters in a substring of this sequence. The substring starts at a specified index start_point and extends to the character at the index end_point. Syntax : public StringBuffer delete(int start_
3 min read
Stack subList() method in Java with Example The subList() method of Java.util.Stack class is used to return a view of the portion of this Stack between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned Stack is empty.) The returned Stack is backed by this Stack, so non-structural cha
3 min read
Stream skip() method in Java with examples Prerequisite : Streams in java The skip(long N) is a method of java.util.stream.Stream object. This method takes one long (N) as an argument and returns a stream after removing first N elements. skip() can be quite expensive on ordered parallel pipelines, if the value of N is large, because skip(N)
3 min read
Java String contains() Method with Example The String.contains() method is used to search for a sequence of characters within a string. In this article, we will learn how to effectively use the string contains functionality in Java.Example:In this example, we check if a specific substring is present in the given string.Java// Java Program to
3 min read
Java String lastIndexOf() Method with Examples The String lastIndexOf() method returns the position of the last occurrence of a given character or substring in a String. The Index starts from 0, and if the given substring is not found it returns -1.In this article, we will learn how to use the string lastIndexOf() method in Java to find the last
4 min read
StringBuffer lastIndexOf() method in Java with Examples In StringBuffer class, there are two types of lastIndexOf() method depending upon the parameters passed to it. lastIndexOf(String str) The lastIndexOf(String str) method of StringBuffer class is the inbuilt method used to return the index within the String for last occurrence of passed substring as
4 min read