Question

String.subSequence() has the following javadoc:

Returns a new 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

str.substring(begin, end) 

This method is defined so that the String class can implement the CharSequence interface.

Can anyone explain?

Was it helpful?

Solution 3

Using str.subSequence(begin, end) returns a CharSequence which is a read-only form of the string represented as a sequence of chars. For example:

String string = "Hello";
CharSequence subSequence = string.subSequence(0, 5);

It's read only in the sense that you can't change the chars within the CharSequence without instantiating a new instance of a CharSequence.

If you have to use str.subSequence(begin, end), you can cast the result to a String:

String string = "Hello";
String subSequence = (String) string.subSequence(0, 5);

and use all the normal String operators like subSequence += " World";

OTHER TIPS

To understand this, the very first thing you need to know is that what is the difference between substring and subsequence

substring is a continuous part or subpart of a string

whereas

subsequence is the part of a string or sequence, that might be continuous or not but the order of the elements is maintained

For example, let's say we have the following strings:

str_a="hello there"
str_b="hello"
str_c="ello th"
str_d="hllo"
str_e="ho hre"
str_f="there hello"

str_b is a substring of str_a, str_c is also a substring of str_a but str_d is not a substring of str_a as this substring is not continuous.

Now all substrings are subsequences as the order is maintained.

str_d is a subsequence of str_a, str_e is also a subsequence of str_a however str_f is not a subsequence of str_a as in this case the order is not maintained.

Now for java, there is no appropriate clarification regarding these methods in javadoc.

Subsequence

Subsequence is a generalisation of substring, suffix, and prefix. Finding the longest string which is a subsequence of two or more strings is known as the longest common subsequence problem.

Example: The string "anna" is a subsequence of the string "banana":

banana
 || ||
 an na

Substring

A substring of a string is a prefix of a suffix of the string, and equivalently a suffix of a prefix. If one string is a substring of another, it is also a subsequence, which is a more general concept.

Example: The string "ana" is a substring (and subsequence) of banana at two different offsets:

banana
 |||||
 ana||
   |||
   ana

Read more here.

But as far as Java is concerned, there isn't any difference in their use as stated clearly in the javadoc. Also as it's stated in there, the method subSequence has only been implemented in class String so as to keep it compliant with the CharSequence interface. And this method's name is indeed just a misnomer.

In simple terms of understanding:

Substring : A substring is a contiguous sequence of characters within a string, where oder matters.

Substring
string
ring

Subsequences: From a string, any of the character but in sequence.

Substring
u s i
s b t i n g
r g

Substrings are consecutive subsequences.

For example, for sequence abc we have

substrings: a, ab, abc, b, bc, c, and the empty substring;
subsequences: a, b, ab, c, ac, bc, abc, and the empty subsequence.

When the letters are repeated, some substrings and subsequences will look the same.

If you look at the source of String, you will see that subsequence actually calls substring. The only thing it does different than substring is it casts the string to a CharSequence.

In short, it doesn't matter much for String objects. They are equivalent, unless you need to pass the result to something which only takes String, in which case you must use the subtring(int,int) version or cast the subsequence(int,int) version to a String (because that's what it is, internally).

String.substring(int,int) existed first, because String existed first. In Java 1.4, CharSequence was added, and somebody apparently realized that it would be useful to have the same functionality there, but which returned a CharSequence instead of String (for flexibility). So, along came CharSequence.subSequence(int,int). However, since String implements CharSequence, String inherited the seemingly redundant method.

My answer is off the code; to for understanding.

Let’s start with what we mean by “sequence” and “subsequence.”

Sequence

  • A sequence is a list of items in which the order of items matters.

  • For example, the string GACA contains the same character (A) multiple times, and it differs from the string CAAG, which has the same population of characters.

Subsequence

  • A subsequence Z of a string X is X, possibly with items removed.

  • For example, if X is the string GAC, then it has eight subsequences:

    1: GAC (no characters removed)

    2: GA (C removed),

    3: GC (A removed),

    4: AC (G removed),

    5: G (A and C removed),

    6: A (G and C removed),

    7: C (G and A removed), and

    8: the empty string (all characters removed).

  • For example, if X is the string CATCGA and Y is the string GTACCGTCA, then CCA is a common subsequence of X and Y consisting of three characters.

Note 1: It is not a longest common subsequence (LCS), however, since the common subsequence CTCA has four characters.

Note 2: CTCA is a longest common subsequence, but it is not unique, since TCGA is another common subsequence with four characters.

Substring

  • a substring is a subsequence of a string in which the characters must be drawn from contiguous positions in the string.
  • For example the string CATCGA, the subsequence ATCG is a substring but the subsequence CTCA is not.

Basically it is just what is returned. One a String and the other a CharSequence (which you can cast as a string).
You would use the subSequence when you need a CharSequence type of variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top