String class 中的 Substring 方法到达了它不应该到达的索引
Substring method in String class reaches the index it isn't supposed to
如果标题不清楚,我深表歉意。
现在,在字符串中索引从 0 开始。例如:
Index 0 1 2 3 4
String H E L L O
在这种情况下,最后一个索引是 4。
如果我尝试做这样的事情:
System.out.println("hello".charAt(5));
它应该会抛出一个 'Index Out Of Bounds Exception'。
但是,如果我尝试 运行 以下代码:
System.out.println("hello".substring(5));
确实如此 运行!我写过类似的代码,后来注意到了这个,想不通。
最后,问题是我们怎么可能达到我们不应该达到的指数?它允许我们写:
stringObj.substring(stringObj.length());
它看起来不太安全,因为 stringObj.length() return 我们使用一个比最后一个索引多 1 的整数。
它是故意制作的吗?这是有目的的吗?如果有,这个目的是什么,带来的好处是什么?
It would not compile due to 'Index Out Of Bounds Exception',
编译通过。当您尝试执行代码时出现运行时异常。
System.out.println("hello".substring(5));
Eventually, the question is that how is this possible that we can reach this index we should not?
阅读 substring(...)
方法的 API。它解释了该方法的工作原理以及参数的含义。
在这种情况下,它 returns 一个空字符串。
参考Java处的substring方法 String API
(http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int))
字符串“.length()”方法将 return 字符串中的字符数。
如果你从 0->4 数 "HELLO",它仍然是 5 个字符(长度),这是你调用 "Hello" 时 return 的数字。长度().
如果要访问字符串的最后一个字符,请使用 (Stringobj.length()-1);通过使用其 .length() 或 .size() 方法调用访问字符串或数组的最后一个元素已在 Java.
中标准化
在这种情况下,了解 charAt 和 substring 方法的实现细节很有帮助。
this tutorial 中描述的 charAt 可以描述为以下方法:
returns the character located at the String's specified index. The string indexes start from zero.
这就是调用的原因:
System.out.println("hello".charAt(5));
给予
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 5
因为正如您完美描述的那样,最大索引只有 4。
substring
在 java 文档中进行了描述,并在 "Examples" 部分解释了您的确切情况 here,其中指出:
"emptiness".substring(9) returns "" (an empty string)
作为额外参考,这里是 java's substring(int index)
实现的源代码,您可以在其中看到它使用了 java 的 substring(int beginInndex, int endIndex)
方法。
当你想到这样的索引时,使用 substring
会更容易(比如字母在 之间 索引)
H E L L O
0 1 2 3 4 5 <- acceptable range of indexes for "HELLO"
^ ^
| |
start end = length
(min index) (max index)
当您从 start
到 end
进行子字符串化时,您只会得到这些索引之间的字符。
所以在 "HELLO".substring(2,4)
的情况下,您将获得部分
L L
2 3 4
其中 returns "LL"
。
现在在 substring(5)
的情况下,它的行为与 substring(5,length)
相同,这意味着在这种情况下它是 substring(5,5)
。因此,由于 5
索引 在我们的 "model" 中存在 作为可接受的值,并且由于 5
和 5
之间没有字符,我们是结果得到空字符串。
类似的情况会发生在 substring(0,0)
substring(1,1)
等情况下,只要我们的模型可以接受索引。
StringIndexOutOfBoundsException
仅当我们尝试访问不可接受的索引时才会发生,这意味着:
- 负数:
-1
、-2
、...
- 大于
length
。这里:6
7
, ...
如果标题不清楚,我深表歉意。
现在,在字符串中索引从 0 开始。例如:
Index 0 1 2 3 4
String H E L L O
在这种情况下,最后一个索引是 4。
如果我尝试做这样的事情:
System.out.println("hello".charAt(5));
它应该会抛出一个 'Index Out Of Bounds Exception'。
但是,如果我尝试 运行 以下代码:
System.out.println("hello".substring(5));
确实如此 运行!我写过类似的代码,后来注意到了这个,想不通。
最后,问题是我们怎么可能达到我们不应该达到的指数?它允许我们写:
stringObj.substring(stringObj.length());
它看起来不太安全,因为 stringObj.length() return 我们使用一个比最后一个索引多 1 的整数。
它是故意制作的吗?这是有目的的吗?如果有,这个目的是什么,带来的好处是什么?
It would not compile due to 'Index Out Of Bounds Exception',
编译通过。当您尝试执行代码时出现运行时异常。
System.out.println("hello".substring(5));
Eventually, the question is that how is this possible that we can reach this index we should not?
阅读 substring(...)
方法的 API。它解释了该方法的工作原理以及参数的含义。
在这种情况下,它 returns 一个空字符串。
参考Java处的substring方法 String API (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int))
字符串“.length()”方法将 return 字符串中的字符数。
如果你从 0->4 数 "HELLO",它仍然是 5 个字符(长度),这是你调用 "Hello" 时 return 的数字。长度().
如果要访问字符串的最后一个字符,请使用 (Stringobj.length()-1);通过使用其 .length() 或 .size() 方法调用访问字符串或数组的最后一个元素已在 Java.
中标准化在这种情况下,了解 charAt 和 substring 方法的实现细节很有帮助。
this tutorial 中描述的 charAt 可以描述为以下方法:
returns the character located at the String's specified index. The string indexes start from zero.
这就是调用的原因:
System.out.println("hello".charAt(5));
给予
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 5
因为正如您完美描述的那样,最大索引只有 4。
substring
在 java 文档中进行了描述,并在 "Examples" 部分解释了您的确切情况 here,其中指出:
"emptiness".substring(9) returns "" (an empty string)
作为额外参考,这里是 java's substring(int index)
实现的源代码,您可以在其中看到它使用了 java 的 substring(int beginInndex, int endIndex)
方法。
当你想到这样的索引时,使用 substring
会更容易(比如字母在 之间 索引)
H E L L O
0 1 2 3 4 5 <- acceptable range of indexes for "HELLO"
^ ^
| |
start end = length
(min index) (max index)
当您从 start
到 end
进行子字符串化时,您只会得到这些索引之间的字符。
所以在 "HELLO".substring(2,4)
的情况下,您将获得部分
L L
2 3 4
其中 returns "LL"
。
现在在 substring(5)
的情况下,它的行为与 substring(5,length)
相同,这意味着在这种情况下它是 substring(5,5)
。因此,由于 5
索引 在我们的 "model" 中存在 作为可接受的值,并且由于 5
和 5
之间没有字符,我们是结果得到空字符串。
类似的情况会发生在 substring(0,0)
substring(1,1)
等情况下,只要我们的模型可以接受索引。
StringIndexOutOfBoundsException
仅当我们尝试访问不可接受的索引时才会发生,这意味着:
- 负数:
-1
、-2
、... - 大于
length
。这里:6
7
, ...