【问题标题】:java substring not working as expectedjava子字符串没有按预期工作
【发布时间】:2018-11-12 07:03:28
【问题描述】:

我正在尝试在java中使用substring函数,但它总是抛出错误,我想知道为什么?从逻辑上讲,代码似乎很好,但为什么会抛出这个错误

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

我在文档子字符串中读到的内容需要 2 个参数

substring(whereIwantToStart,howManyCharactersToshow)

下面是我的代码

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

有人可以解释一下有什么问题吗?请我需要解释。谢谢:)

【问题讨论】:

  • 提示:所有 Java 库类和方法都记录得非常好。您应该始终首先阅读该文档,而不是仅仅调用一个方法。在不知道参数含义的情况下调用方法是没有意义的。
  • 文档中的示例: "hamburger".substring(4, 8) 返回 "urge" "smiles".substring(1, 5) 返回 "mile"
  • 你能提供说“子字符串需要2个参数子字符串(whereIwantToStart,howManyCharactersToshow)”的文件吗?
  • 你必须仔细阅读文档,但不要灰心,实践是大师。一个小提示:如果您使用的是 eclipse,您可以在“test”之前按 CTRL+空格。显示提供上下文、文档和完成选项的内容辅助。祝你好运。

标签: java string substring


【解决方案1】:

doc

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

返回一个新字符串,它是该字符串的子字符串。子字符串从指定的 beginIndex 开始,并且 延伸到索引 endIndex - 1 处的字符。因此 子字符串是 endIndex-beginIndex。

您需要"160994".substring(2, 4) 才能获得09

【讨论】:

    【解决方案2】:

    结束索引应该大于开始索引。要获得 '09' 的输出,您应该提供结束索引为 4 test.substring(2,4);

    返回一个新字符串,它是该字符串的子字符串。
    子字符串从指定的beginIndex 开始并扩展 到索引endIndex - 1处的字符。

    因此子字符串的长度是endIndex-beginIndex

    StringIndexOutOfBoundsException 会抛出以下情况

    1. 开始索引
    2. endIndex > value.length
    3. endIndex - beginIndex

    【讨论】:

      【解决方案3】:

      供您所需的输出使用-

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

      【讨论】:

        【解决方案4】:

        这是java中子字符串的格式

        public String substring(int beginIndex, int endIndex)
        

        您指定从索引 2 开始并在索引 1 结束,这就是它抛出异常索引超出范围的原因。

        要获得 09 的输出,您需要

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

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

        【讨论】:

          【解决方案5】:

          public String substring(int startIndex, int endIndex):此方法返回新的String对象,其中包含给定字符串从指定startIndex到endIndex的子字符串。

          让我们通过下面给出的代码来理解 startIndex 和 endIndex。

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

          输出:he

          注意:

          endIndex > startIndex

          在您的情况下:在 1 和 2 位置之间更改为

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

          输出:09

          【讨论】:

            【解决方案6】:

            字符串测试 = "160994";

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

            子字符串表示 (beginIndex, endIndex) 和 endIndex 应该大于 beginIndex。并且你的 value(09) 应该保持在 beninIndex(这是起始索引)和 endIndex(这是最后一个索引)之间。

            并且您已使用 endIndex 1,因此您收到错误,因为您的 beginIndex 大于 endIndex。

            如果你想得到 Ans。 09 那么你应该把 endIndex 4.

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2019-08-01
              • 1970-01-01
              • 2023-04-01
              • 1970-01-01
              • 2023-04-03
              • 1970-01-01
              • 1970-01-01
              • 2017-01-06
              相关资源
              最近更新 更多