Java 子字符串(int)方法神奇地不';t抛出StringIndexOutOfBoundsException

Java 子字符串(int)方法神奇地不';t抛出StringIndexOutOfBoundsException,java,Java,为什么“a”.substring(1)不抛出StringIndexOutOfBoundsException,而它对大于等于2的索引抛出?真的很有趣 提前谢谢 您将在源代码中得到答案: public String substring(int beginIndex) { if (beginIndex < 0) { throw new StringIndexOutOfBoundsException(beginIndex); }

为什么
“a”.substring(1)
抛出
StringIndexOutOfBoundsException
,而它对大于等于2的索引抛出?真的很有趣


提前谢谢

您将在源代码中得到答案:

 public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }
这将变成:

int subLen = 1 - 1;

subLen
将为0,因此
如果(subLen<0){
将为false,并且不会引发异常:)

您将在源代码中得到答案:

 public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }
这将变成:

int subLen = 1 - 1;

subLen
将为0,因此
如果(subLen<0){
将为false,并且它不会引发异常:)

,因为您将返回一个空字符串,即
“”

}

O/p:

当int参数大于字符串的长度时,您会得到
StringIndexOutOfBoundsException

例如:


System.out.println(“a”.substring(2).equals(“”);//抛出StringIndexOutOfBoundsException

,因为您得到了一个空字符串,即,

}

O/p:

当int参数大于字符串的长度时,您会得到
StringIndexOutOfBoundsException

例如:


System.out.println(“a”。子字符串(2)。等于(“”);//抛出StringIndexOutOfBoundsException
子字符串(1)
返回一个空字符串,这就是它不抛出异常的原因,如前面所述<代码>字符串。子字符串(n)
仅当n大于字符串长度时才会失败。

a.substring(1)
返回一个空字符串,这就是它不会引发异常的原因,如所述<代码>字符串。子字符串(n)
仅当n大于字符串长度时才会失败。

看看
子字符串()的作用

 public String substring(int beginIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    int subLen = value.length - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
公共字符串子字符串(int beginIndex){
如果(beginIndex<0){
抛出新的StringIndexOutOfBoundsException(beginIndex);
}
int SUBELN=value.length-beginIndex;
如果(子项<0){
抛出新StringIndexOutOfBoundsException(SubCN);
}
返回(beginIndex==0)?这是:新字符串(值,beginIndex,子项);
}

int subcn=value.length-beginIndex
if
subLen看看
substring()
的作用

 public String substring(int beginIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    int subLen = value.length - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
公共字符串子字符串(int beginIndex){
如果(beginIndex<0){
抛出新的StringIndexOutOfBoundsException(beginIndex);
}
int SUBELN=value.length-beginIndex;
如果(子项<0){
抛出新StringIndexOutOfBoundsException(SubCN);
}
返回(beginIndex==0)?这是:新字符串(值,beginIndex,子项);
}
int subcn=value.length-beginIndex如果
subLen

抛出
索引自动边界异常
-如果
开始索引
为负值或 大于此字符串对象的长度

另外,根据JavaDoc中的示例:

“空”。子字符串(9)
返回
(空字符串)

因此,
“a”。子字符串(1)
返回一个空字符串,它应该返回。

public static void main(String[] args) {
System.out.println("a".substring(1).equals("")); 
}

You got that from the source code but one point to think is why java has :

You get StringIndexOutOfBoundsException when the int argument is greater that length of the String.


why not throw exception when int argument is equal to length of string because...

String substring(int fromindex); <-prototype

There is no point of NOT throwing exception for int argument '1' as String length is 1 and you are searching from second index..

can anyone give reason? 
抛出
索引自动边界异常
-如果
开始索引
为负值或 大于此字符串对象的长度

另外,根据JavaDoc中的示例:

“空”。子字符串(9)
返回
(空字符串)

因此,
“a”。子字符串(1)
返回一个空字符串,它应该返回空字符串。

公共静态void main(字符串[]args){
public static void main(String[] args) {
System.out.println("a".substring(1).equals("")); 
}

You got that from the source code but one point to think is why java has :

You get StringIndexOutOfBoundsException when the int argument is greater that length of the String.


why not throw exception when int argument is equal to length of string because...

String substring(int fromindex); <-prototype

There is no point of NOT throwing exception for int argument '1' as String length is 1 and you are searching from second index..

can anyone give reason? 
System.out.println(“a”。子字符串(1)。等于(“”); } 您可以从源代码中了解到这一点,但有一点需要思考,那就是为什么java有: 当int参数大于字符串的长度时,您将获得StringIndexOutOfBoundsException。 为什么不在int参数等于字符串长度时引发异常,因为。。。 字符串子字符串(int-fromindex)<代码>公共静态void main(字符串[]args){ System.out.println(“a”。子字符串(1)。等于(“”); } 您可以从源代码中了解到这一点,但有一点需要思考,那就是为什么java有: 当int参数大于字符串的长度时,您将获得StringIndexOutOfBoundsException。 为什么不在int参数等于字符串长度时引发异常,因为。。。
字符串子字符串(int-fromindex);这是我需要看到的答案,非常感谢:)这是我需要看到的答案,非常感谢:)