gpt4 book ai didi

Java String.subsequence() 方法

转载 作者:行者123 更新时间:2023-11-29 03:54:19 25 4
gpt4 key购买 nike

比如说,String str = "hello world";要,打招呼,我们可以使用 str.subSequence(0, 5)。如果它是一个从 0 开始的索引字符串,那么为什么我们不把 str.subSequence(0.4) 写成 'o' 的索引是 4?

最佳答案

方法请看javadoc。

public CharSequence subSequence(int beginIndex,int endIndex)

返回一个新的字符序列,它是这个序列的子序列。调用此表单的方法

 str.subSequence(begin, end)

行为方式与调用完全相同

 str.substring(begin, end)

定义这个方法是为了String类可以实现CharSequence接口(interface)。

指定者:接口(interface) CharSequence 中的子序列

参数:beginIndex - 开始索引,包括在内。endIndex - 结束索引,独占。

返回:指定的子序列。

抛出:IndexOutOfBoundsException - 如果 beginIndex 或 endIndex 为负数,如果 endIndex 大于 length(),或者如果 beginIndex 大于 startIndex

关于Java String.subsequence() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7219157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com