简体   繁体   English

java子字符串无法按预期工作

[英]java substring not working as expected

I'm trying to use substring function in java, but it keeps throwing an error, I want to know why ? 我正在尝试在Java中使用子字符串函数,但是它一直抛出错误,我想知道为什么? the code seems to be good logically speaking but why it is throwing this error 从逻辑上讲,该代码似乎很好,但是为什么会引发此错误

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1

What I've read in the documentation substring takes 2 parameter 我在文档子字符串中阅读的内容带有2个参数

substring(whereIwantToStart,howManyCharactersToshow)

below is my code 下面是我的代码

    String test = "160994";
    System.out.println(test.substring(2,1)); //output should be 09 why error?

Can someone explain me what is wrong ? 有人可以解释我哪里错了吗? please I need explanation. 请我解释一下。 Thanks :) 谢谢 :)

See the doc : 参见文档

public String substring(int beginIndex, int endIndex) 公共字符串子字符串(int beginIndex,int endIndex)

Returns a new string that is a substring of this string. 返回一个新字符串,该字符串是该字符串的子字符串。 The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex. 子字符串从指定的beginIndex开始,并扩展到索引endIndex-1处的字符。因此,子字符串的长度为endIndex-beginIndex。

You need "160994".substring(2, 4) to get 09 . 您需要"160994".substring(2, 4)来获取09

对于您所需的输出,请使用-

System.out.println(test.substring(2,4)); 

This is the format for the substring in java 这是Java中子字符串的格式

public String substring(int beginIndex, int endIndex)

you are specifying start from index 2 and end at index 1 , that's why it's throwing an exception index out of range. 您指定从索引2开始到索引1结束,这就是为什么它将异常索引抛出范围之外的原因。

To get the output as 09 you need 要获得09的输出,您需要

 System.out.println(test.substring(2,4));

Appendix - Java Docs https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int) 附录-Java文档https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int,%20int)

End index should be greater than the Start Index. 结束索引应大于开始索引。 To get output as '09', you should provide the end index as 4 test.substring(2,4); 要获得输出为'09',您应该提供结束索引为4 test.substring(2,4);

Returns a new string that is a substring of this string. 返回一个新字符串,该字符串是该字符串的子字符串。 The
substring begins at the specified beginIndex and extends to the character at index endIndex - 1 . 子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1处的字符。

Thus the length of the substring is endIndex-beginIndex . 因此,子字符串的长度为endIndex-beginIndex

The StringIndexOutOfBoundsException will throw in below cases 在以下情况下,将抛出StringIndexOutOfBoundsException

  1. beginIndex < 0 beginIndex <0
  2. endIndex > value.length endIndex> value.length
  3. endIndex - beginIndex < 0 endIndex-beginIndex <0

public String substring(int startIndex, int endIndex) : This method returns new String object containing the substring of the given string from specified startIndex to endIndex. public String substring(int startIndex, int endIndex) :此方法返回新的String对象,其中包含给定字符串从指定的startIndex到endIndex的子字符串。

Let's understand the startIndex and endIndex by the code given below. 让我们通过下面给出的代码了解startIndex和endIndex。

String s="hello";  
System.out.println(s.substring(0,2)); 

Output : he 输出: he

Notice : 注意 :

endIndex > startIndex endIndex> startIndex

in your case : change between 1 and 2 place to 在您的情况下:在1到2之间更改

String test = "160994";
System.out.println(test.substring(2, 4)); //output should be 09

Output : 09 输出: 09

String test = "160994"; 字符串测试=“ 160994”;

System.out.println(test.substring(2,1)); 的System.out.println(test.substring(2,1));

Substring means (beginIndex, endIndex) and endIndex Should be larger than beginIndex.and your value(09) should be stay between beninIndex(which is starting index) and endIndex(Which is last index). 子字符串的意思是(beginIndex,endIndex)和endIndex应该大于beginIndex。并且您的value(09)应该保持在beninIndex(这是起始索引)和endIndex(这是最后一个索引)之间。

and you have taken endIndex 1 so you are geting the Error because your beginIndex is larger than endIndex. 并且您采用了endIndex 1,因此由于您的beginIndex大于endIndex而出现错误。

if you want to get Ans. 如果你想得到Ans。 09 then you should have to put endIndex 4. 09,则您必须放置endIndex 4。

Line will be:-System.out.println(test.substring(2,4)); 行将是:-System.out.println(test.substring(2,4));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM