Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between String.subString() and String.subSequence()

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?

like image 432
code511788465541441 Avatar asked Mar 19 '13 16:03

code511788465541441


People also ask

What is difference between string and 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.

What is string subsequence method?

subSequence() is a built-in function in Java that returns a CharSequence. CharSequence that is a subsequence of this sequence. The subsequence starts with the char value at the specified index and ends with the char value at (end-1).

What is the difference between Subarray and subsequence?

A subarray or substring will always be contiguous, but a subsequence need not be contiguous. That is, subsequences are not required to occupy consecutive positions within the original sequences. But we can say that both contiguous subsequence and subarray are the same.

What is substring in data structure?

In formal language theory and computer science, a substring is a contiguous sequence of characters within a string. For instance, "the best of" is a substring of "It was the best of times".


2 Answers

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.

like image 168
Nishchay Avatar answered Oct 16 '22 22:10

Nishchay


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.

like image 33
Amar Avatar answered Oct 16 '22 23:10

Amar