在 Java 中,你可以使用负的子字符串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19647077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Java, can you use negative one with substring?
提问by Slight
Can negative one be used as the end index for string.substring in Java?
负一可以用作Java中string.substring的结束索引吗?
Example:
例子:
String str = "test";
str.substring(0, str.indexOf("q"));
Edit: Nowhere in the javadocs does it say directly that endindexcannot be negative. There are implementations of substring in other languages and libraries that allow a negative endindex but disallowa negative beginindex, so it seems relevant that this be explicitly stated. It is not in any way implied either. (Edit: ok it is implied loosely, but I and apparently others who have asked me this question in person still find it pretty unclear. This was meant to be a simple Q+A that I provided not me actually trying to find an answer to this trivial question)
编辑:javadocs 中没有任何地方直接说endindex不能为负数。其他语言和库中的 substring 实现允许负的结束索引但不允许负的开始索引,因此明确说明这一点似乎是相关的。它也没有以任何方式暗示。(编辑:好的,它是松散的暗示,但我和显然其他人亲自问过我这个问题仍然觉得很不清楚。这是一个简单的问答,我提供的不是我实际上试图找到答案的这个微不足道的问题)
采纳答案by user2864740
No. Negative indices are notallowed.
没有。负指数不会允许的。
From String#substring(int beginIndex, int endIndex):
从String#substring(int beginIndex, int endIndex):
Throws: IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
抛出: IndexOutOfBoundsException - 如果beginIndex 为负,或 endIndex 大于此 String 对象的长度,或beginIndex 大于 endIndex。
While the documentation does not directly state that endIndex cannot be negative1, this can be derived. Rewriting the relevant requirements yields these facts:
虽然文档没有直接说明 endIndex 不能为负1,但可以推导出来。重写相关要求会产生以下事实:
- "beginIndex cannot be negative" ->
beginIndex >= 0
- "beginIndex must be smaller than or equal to endIndex" ->
endIndex >= beginIndex
- “beginIndex 不能为负数”->
beginIndex >= 0
- “beginIndex 必须小于或等于 endIndex” ->
endIndex >= beginIndex
Thus it is a requirement that endIndex >= beginIndex >= 0
which means that endIndex cannotbe negative.
因此,要求endIndex >= beginIndex >= 0
endIndex不能为负数。
Anyway, str.substring(0, -x)
can be trivially rewritten as str.substring(0, str.length() - x)
, assuming we have the same idea of what the negative end index should mean. The original bound requirements still apply of course.
无论如何,str.substring(0, -x)
可以简单地重写为str.substring(0, str.length() - x)
,假设我们对负端索引的含义有相同的想法。当然,最初的约束要求仍然适用。
1Curiously, String#subSequence
doesexplicitly forbid a negative endIndex. Given such, it feels that the documentation could be cleaned up such that both methods share the same simplified precondition text. (As a bonus: there is also an important typo in the "Java 7" subSequence documentation.)
1奇怪的是,确实明确禁止负的 endIndex。鉴于此,感觉可以清理文档,使两种方法共享相同的简化前提条件文本。(作为奖励:“Java 7”子序列文档中还有一个重要的错字。)String#subSequence
回答by Slight
No, using negative numbers for the endindex of substring as well as a number greater than the string length will result in an StringIndexOutOfBoundsException.
不,使用负数作为子字符串的结束索引以及大于字符串长度的数字将导致 StringIndexOutOfBoundsException。
(You wouldn't believe how hard it was to find a straight answer to this on the net)
(你不会相信在网上找到一个直接的答案是多么困难)