In Java, a string object is an object that represents a sequence of characters. A string object is created using the String class, which is a built-in class in Java. Strings are used to represent text data in Java. They are a sequence of characters and can be used to store and manipulate words, sentences, and other types of text.
The Java platform provides a rich set of classes and methods for working with strings, making it easy to perform common tasks such as searching, parsing, and formatting strings.
To know more about String Class in java read the complete article.
String subSequence() method.
The subSequence method is a built-in method of the CharSequence interface in Java, which is implemented by the String class. The subSequence method returns a new string that is a subsequence of the original string, starting at a specified index and ending at another specified index.
The beginIndex is the index of the first character in the subsequence, and the endIndex is the index of the character following the last character in the subsequence. The subSequence method includes the character at the beginIndex, but excludes the character at the endIndex.
Returns Returns a new character sequence that is a subsequence of this sequence.
Throws: IndexOutOfBoundsException - if beginIndex or endIndex are negative, if endIndex is greater than length(), or if beginIndex is greater than startIndex
public CharSequence subSequence(int beginIndex,int endIndex)
Parameters:
beginIndex - the begin index, inclusive.
endIndex - the end index, exclusive.
Let’s look at a string-related Java program where the Java String subSequence() Method is used to perform an operation on the given string.
Example: Java String subsequence() Method
public class Main{
public static void main(String[] args) {
String str = "Hello World";
// Extract a subsequence starting at index 6 and ending at index 11
CharSequence subSequence = str.subSequence(6, 11);
System.out.println(subSequence);
}
}
Output
World
Explanation:The method takes two arguments: the start index (inclusive) and the end index (exclusive) of the subsequence. The start index must be greater than or equal to 0 and less than or equal to the length of the string, and the end index must be greater than or equal to the start index and less than or equal to the length of the string.
public class Main{
public static void main(String[] args) {
String str = "Hello World";
// Extract a subsequence starting at index 6 and ending at index 11
CharSequence subSequence = str.subSequence(6, 11);
// Print the length of the subsequence
System.out.println("Length: " + subSequence.length()); // Outputs "Length: 5"
// Convert the subsequence to a string
String subString = subSequence.toString();
System.out.println("String: " + subString); // Outputs "String: World"
// Check if the subsequence is equal to another string
String compareString = "World";
System.out.println("Equal: " + subSequence.equals(compareString)); // Outputs "Equal: true"
// Get the character at a specific index in the subsequence
char c = subSequence.charAt(2);
System.out.println("Char at index 2: " + c); // Outputs "Char at index 2: r"
}
}
Output
Length: 5
String: World
Equal: true
Char at index 2: r
Explanation:This code demonstrates some additional capabilities of the subSequence() method beyond just extracting a subsequence from a string. It shows how to get the length of the subsequence, convert it to a string, compare it to another string, and access individual characters within the subsequence.
The length() method returns the number of characters in the subsequence. The toString() method returns a string representation of the subsequence. The equals() method compares the subsequence to another object (in this case, a string) and returns true if they are equal and false if they are not. The charAt() method returns the character at a specific index in the subsequence.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment