Java 1.4 introduced the CharSequence interface and String implements this interface. This is the only reason for the implementation of subSequence() method in String class. Internally it invokes the substring() method.

Java String subSequence

java-string-subsequence

Below code snippet is from String subSequence method implementation.

String subSequence method returns a character sequence that is a subsequence of this sequence. An invocation of this method of the form str.subSequence(begin, end) behaves in exactly the same way as the invocation of str.substring(begin, end).

Below is a simple Java String subSequence method example.

StringSubsequence.java

Output of the above String subSequence example program is:

There is no benefit in using subSequence method. Ideally, you should always use String substring method.

By admin

Leave a Reply