gpt4 book ai didi

java - 子串奇怪的界限?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:44:20 26 4
gpt4 key购买 nike

<分区>

所以,我想弄清楚为什么 String.substring(int startIndex) 允许起始索引超出范围并且不会抛出 OOB 异常?

这是我用来测试的代码:

public class testSub {
public static void main(String[] args) {
String testString = "D";
String newSub= testString.substring(1); //print everything FROM this index Up, right? Should that not be out of bounds? Yet I just get a blank.
System.out.println(newSub); //this works fine and prints a blank
System.out.println(testString.charAt(1)); // <Yet this throws OOB?
System.out.println(testString.lastIndexOf("")); // Gives me (1) but I just tried getting it? Should this not mean String length is 2?
}
}

我明白子串是子串(包括,不包括),但是1显然是越界的,那么为什么它给出一个空格而不是抛出OOB,或者它是怎么做到的? “”是一些特殊的异常(exception)吗?

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