java - String.subString() 和 String.subSequence() 有什么区别

标签 java string substring subsequence

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.

谁能解释一下?

最佳答案

要理解这一点,首先你需要知道的是substringsubsequence

有什么区别

子字符串连续部分或字符串的子部分

子序列是字符串或序列的一部分,它可能是连续的或不连续的,但元素的顺序保持不变

例如,假设我们有以下字符串:

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

str_bstr_a 的子串,str_c 也是 str_astr_d 的子串 不是 str_a 的子字符串,因为该子字符串不是 连续的

现在所有子字符串都是子序列,因为顺序保持不变。

str_dstr_a 的子序列,str_e 也是 str_a 的子序列 但是 str_f 不是 str_a 的子序列,因为在这种情况下 order 不被维护。

现在对于 java,javadoc 中没有关于这些方法的适当说明。

关于java - String.subString() 和 String.subSequence() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15505508/

相关文章:

java - 一对多级联保存,错误 : null value in column "b_id" violates not-null constraint

c - 大输入数频程序

Python:是否有快捷方式来查找哪个子字符串(来自一组子字符串)在字符串中排在第一位?

Java 在字符串中搜索子字符串除非前面有特定字符

algorithm - 长度为 X 的最常见子串

java - 重绘方法不起作用?

java - Java Spring 中受控的 ThreadPoolExecutor

Java jasper 报告列表作为数据源

python - 有没有更好的方法从 python 3 中的字符串中提取数字

php - 在php中将从MySQL查询获得的string/val转换为int?