java子字符串和字符返回&引用;在读取最后一个字符时分别执行和异常 公共类StringDemo{ 公共静态void main(字符串[]args){ String str=“this:”; int a=str.indexOf(“:”;//返回4 String subStr=str.substring(a+1);//返回“” String subStr=str.substring(a+2);//引发异常 int charAt=str.charAt(a+1);//抛出一个StringIndexOutofBoundsXP。 } }

java子字符串和字符返回&引用;在读取最后一个字符时分别执行和异常 公共类StringDemo{ 公共静态void main(字符串[]args){ String str=“this:”; int a=str.indexOf(“:”;//返回4 String subStr=str.substring(a+1);//返回“” String subStr=str.substring(a+2);//引发异常 int charAt=str.charAt(a+1);//抛出一个StringIndexOutofBoundsXP。 } },java,string,char,substring,Java,String,Char,Substring,有人能解释它为什么返回“”和为什么抛出异常吗?子字符串(a+1)从a+1或5开始从str返回子字符串。您的字符串在索引5处没有任何内容,因此它将返回一个空字符串。str.substring(a+1)从str返回从a+1或5开始的子字符串。您的字符串在索引5处没有任何内容,因此它将返回一个空字符串 public class StringDemo{ public static void main(String[] args) { String str = "this:";

有人能解释它为什么返回“”和为什么抛出异常吗?子字符串(a+1)从
a+1或
5开始从
str
返回子字符串。您的字符串在索引
5
处没有任何内容,因此它将返回一个空字符串。

str.substring(a+1)
str
返回从
a+1
5
开始的子字符串。您的字符串在索引
5
处没有任何内容,因此它将返回一个空字符串

public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}
str.substring(a+1)
str.charAt(a+1)
返回给定索引(
a+1
)后面的字符串,即冒号后面的字符串,冒号是空字符串

public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}
str.substring(a+1)
str.charAt(a+1)
访问冒号后面不存在的位置处的数组值

返回给定索引(
a+1
)后面的字符串,即冒号后面的字符串,冒号是空字符串

public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}
str.substring(a+1)
str.charAt(a+1)

访问冒号后面不存在的位置的数组值。

否它不会抛出
IndexOutOfBoundException
,而是返回空字符串。 对于第二种方法,当
beginIndex
endIndex
相等时,情况也是如此。当
beginIndex
为负数、大于
endIndex
或大于字符串的
长度时,它只会抛出
StringIndexBoundException

String subStr=str.substring(a+1);//返回“”。 //因为那地方什么都没有

很明显

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

阅读更多:

其中as
CharAt
抛出:
IndexOutOfBoundsException
-如果索引参数为负数或不小于此字符串的
长度
否,它将不会抛出
IndexOutOfBoundException
,而是返回空字符串。 对于第二种方法,当
beginIndex
endIndex
相等时,情况也是如此。当
beginIndex
为负数、大于
endIndex
或大于字符串的
长度时,它只会抛出
StringIndexBoundException

String subStr=str.substring(a+1);//返回“”。 //因为那地方什么都没有

很明显

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

阅读更多:

其中as
CharAt
抛出:
IndexOutOfBoundsException
-如果索引参数为负或不小于此字符串的
长度

,我想首先回答为什么结果是空字符串

public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}
str.substring(a+1)
str.charAt(a+1)
方法:子字符串(int beginIndex) 就像说substring(beginIndex,String.length())

Method:substring(int beginIndex) 在参数上指定的endIndex是5,长度也是5。因此,如果执行子字符串(0,0)或子字符串(1,1)或子字符串(5,5),它将始终给出一个空字符串,因为开始和结束索引相等。用简单的语言来说,相同的开始和结束索引不占用任何内容,从而导致空字符串

public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}
str.substring(a+1)
str.charAt(a+1)
为什么整数5仍然是有效索引?答案取决于Java API本身:抛出: IndexOutOfBoundsException-如果beginIndex为负值或大于此字符串对象的长度

参考:



我想首先回答为什么结果是空字符串

public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}
str.substring(a+1)
str.charAt(a+1)
方法:子字符串(int beginIndex) 就像说substring(beginIndex,String.length())

Method:substring(int beginIndex) 在参数上指定的endIndex是5,长度也是5。因此,如果执行子字符串(0,0)或子字符串(1,1)或子字符串(5,5),它将始终给出一个空字符串,因为开始和结束索引相等。用简单的语言来说,相同的开始和结束索引不占用任何内容,从而导致空字符串

public class StringDemo{

   public static void main(String[] args) {

       String str = "this:";
       int a = str.indexOf(":"); // returns 4
       String subStr = str.substring(a+1); // returns "" <empty string>
       String subStr = str.substring(a+2); // throws exception
      int charAt = str.charAt(a+1); // Throws an StringIndexOutOfBoundsExp.
    }
}
str.substring(a+1)
str.charAt(a+1)
为什么整数5仍然是有效索引?答案取决于Java API本身:抛出: IndexOutOfBoundsException-如果beginIndex为负值或大于此字符串对象的长度

参考:



你读过这两种方法的javadoc吗?读过Javadocs:我想知道为什么你读过这两种方法的javadoc吗?读过Javadocs:我想知道为什么你的答案很简单,但是当我试图访问+2位置时,它在子字符串方法中给了我一个异常,所以字符串没有在第6个索引处有任何内容,因此它为什么会引发异常
。substring()
将返回一个空字符串当您使用字符串大小调用它时,它将只返回一个新的空字符串。任何更大的值,它都会抛出异常。好的,我同意你的答案,但是当我尝试访问+2位置时,它在substring方法中给我一个异常,所以string在第6个索引中没有任何内容,所以为什么它会抛出异常
。substring()
将在你用字符串大小调用它时返回一个空白字符串,它只会返回一个新的空字符串。任何更大的,它将抛出一个异常。