This article will teach you how to get a subsequence from a String. This looks similar to the Java String substring() method, but the substring method returns a new String while the subSequence method returns CharSequence rather than String.
The reason that the subSequence() method is implemented in the String class is that Java 1.4 introduced the CharSequence interface and String implements this interface. This method internally invokes the substring() method.
Post On: | String subSequence() Method in Java |
Post Type: | Java Tutorials |
Published On: | www.softwaretestingo.com |
Applicable For: | Freshers & Experience |
Get Updates: | SoftwareTestingo Telegram Group |
In this article, we will show how to use String subSequence in Java Programming language with an example.
Syntax:
The syntax of the subSequence()
method is:
// It will return Character Sequence public CharSequence subSequence(int Startig_index, int End_index); //In order to use in program String_Object.subSequence(int Startig_index, int End_Index)
Here the subSequence() method takes 2 input parameters which should be an integer value. The First parameter informs where the extraction will start and the second parameter informs where the extraction will end.
And the String.subSequence method will return the character sequence starting from the Starting_index up to End_index but not included.
Tips: When we are invoking the subSequence() method it internally invokes the substring() method. And the code snippet is looking something like the below:
public CharSequence subSequence(int beginIndex, int endIndex) { return this.substring(beginIndex, endIndex); }
Example Of Java String subSequence(): The code snippet below demonstrates how to use the subSequence() method. This method returns CharSequence from a string.
package com.SoftwareTestingo.JavaBasics; public class SubSequenceExample { public static void main(String[] args) { String str = "www.SoftwareTestingo.com"; System.out.println("Last 4 char String: " + str.subSequence(str.length() - 4, str.length())); System.out.println("First 4 char String: " + str.subSequence(0, 4)); System.out.println("website name: " + str.subSequence(4, 20)); // substring vs subSequence //When you are compare with == its comparing Hashcode which is different System.out.println("substring == subSequence ? " + (str.substring(4, 14) == str.subSequence(4, 14))); //When you compare with equals(), its comparing value System.out.println("substring equals subSequence ? " + (str.substring(4, 14).equals(str.subSequence(4, 14)))); } }
String subSequence() With Negative Example: In the below example we will try to execute by entering the negative value in the index position.
If you pass a negative value in the index, then you will get StringIndexOutOfBoundsException.
package com.SoftwareTestingO.Java.basics; public class SubSequenceExample2 { public static void main(String[] args) { String stringObject = "This example is with negative index"; CharSequence output = stringObject.subSequence(-10, stringObject.length()); System.out.println(output); } }
Final Words:
I hope now you are able to understand what is the use of subSequence() in Java. Still, if you have any questions you can ask us in the comment section.
What will happen if the index is negative on subSequence()?
Java does not support negative indexes for subSequence() like Python, that’s why it will result in a runtime exception IndexOutOfBoundsException.
What is the difference between substring() and subSequence()?
The substring() method returns a string, while the subSequence() method returns a CharSequence. The main difference is that in the subSequence() method you cannot pass a single start index like you can with the substring() method. Additionally, subSequence()’s internal implementation is based on the substring() method.
Leave a Reply