Java String subSequence() Method
What is a string in Java?
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.
Syntax:
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
Example 2 : Java String subsequence() Method
public class Main{ public static void main(String[] args) { String str = "PrepInsta Prime!"; System.out.println("string = " + str); System.out.println("string subsequence = " + str.subSequence(1,10)); } }
Output
string = PrepInsta Prime! string subsequence = repInsta
Example 3: how to use the subsequence property:
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
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment