gpt4 book ai didi

Java字符串索引越界困惑

转载 作者:行者123 更新时间:2023-12-01 18:06:58 25 4
gpt4 key购买 nike

假设我有一个

String temp = "abcd";

System.out.println(temp.substring(0,4)); // out of bound, no error
System.out.println(temp.substring(0,5)); // out of bound, error
为什么?

最佳答案

Javadocs for this method状态:

The substring begins at the specified beginIndex and extends to the character at index endIndex - 1.

Throws:

IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.

length 的结束索引是可以的,但 length + 1 则不行。

关于Java字符串索引越界困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35662448/

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