String.subSequence()
具有以下 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.
谁能解释一下?
最佳答案
要理解这一点,首先你需要知道的是substring和subsequence
有什么区别子字符串是连续部分或字符串的子部分
而
子序列是字符串或序列的一部分,它可能是连续的或不连续的,但元素的顺序保持不变
例如,假设我们有以下字符串:
str_a="hello there"
str_b="hello"
str_c="ello th"
str_d="hllo"
str_e="ho hre"
str_f="there hello"
str_b
是 str_a
的子串,str_c
也是 str_a
但 str_d 的子串
不是 str_a
的子字符串,因为该子字符串不是 连续的。
现在所有子字符串都是子序列,因为顺序保持不变。
str_d
是 str_a
的子序列,str_e
也是 str_a
的子序列 但是 str_f
不是 str_a
的子序列,因为在这种情况下 order 不被维护。
现在对于 java,javadoc 中没有关于这些方法的适当说明。
关于java - String.subString() 和 String.subSequence() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505508/