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

后端 未结 8 1016
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-25 11:34

String.subSequence() has the following javadoc:

Returns a new character sequence that is a subsequence of this sequence.

相关标签:
8条回答
  • 2020-12-25 12:20

    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.

    0 讨论(0)
  • 2020-12-25 12:26

    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.

    0 讨论(0)
提交回复
热议问题