【问题标题】:String index out of range when working with String Builder使用字符串生成器时字符串索引超出范围
【发布时间】:2019-10-30 07:23:51
【问题描述】:

所以我确实得到了这种类型的错误:

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围: 447

每次我运行这段代码时:

String text = "What is Lorem Ipsum? Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
    StringBuilder textSb = new StringBuilder(text);
    int textLength = textSb.length();
    System.out.println(textLength);
    for(int i = 3; i< textLength; i += 4){
        if (textSb.charAt(i) == ' '){
            textSb.deleteCharAt(i+1);
            i--;
        }else{
            textSb.deleteCharAt(i);
            i--;
        }

好的。所以它想做什么。每次循环第四个字符时,我都会尝试删除,如果是空格,我会删除空格后的第二个字符。我尝试“i--”,因为文本变小,每次只有一个字符,这意味着我的索引将无法正常工作,因为我会不断删除这些字符。

主要问题是那个错误。考虑到我的字符串中有大约 597 个字符,怎么可能 447 不在范围内。

【问题讨论】:

  • 你永远不会重新计算textLength,但你会删除字符......
  • @ElliottFrisch 我尝试制作 textlength++ 并且是同样的错误。 for 是否只获取它的文本长度一次?
  • 这个练习有什么意义?您的预期输出是什么样的?
  • 只需将for循环修改为for(int i = 3; i&lt; textSb.length(); i += 4),结果应该类似于Wha is orm Isum Loem psu is...
  • 您可能需要先检查i+1 是否小于textLength,然后再执行textSb.deleteCharAt(i+1);。您可以在现有的if 条件本身中添加检查:if (textSb.charAt(i) == ' ' &amp;&amp; i+1&lt;textLength)。另一个问题是,当您删除 Elliott 提到的角色时,您需要重新分配 textLength 的值。在每个删除语句后添加textLength = textSb.length();

标签: java stringbuilder


【解决方案1】:

477 是因为您删除了其余的索引。在删除索引之前检查一下

if (textSb.charAt(i) == ' ' && (i+1<textLength)){
            textSb.deleteCharAt(i+1);
            i--;
}

【讨论】:

    【解决方案2】:

    在修改了原始字符串 (textSb) 的长度但使用字符串的原始长度后,您会得到 StringIndexOutOfBoundsException 也就不足为奇了。

    看这个例子:https://github.com/lombocska/stackoverflow/blob/master/src/main/java/string_index_out_of_range/StringBuilderTestClass.java#L35

    for (int i = 3; i < textSb.length(); i += 4) {
      ...
    }
    

    The answer is that you should use the modified text length in the for cycle instead of the unmodified one. :)

    【讨论】:

      【解决方案3】:

      textSb.deleteCharAt(i);是罪魁祸首。您不应该从同一个 StringBuilder 中删除,而是创建它的副本,然后对副本执行操作。

      【讨论】:

        【解决方案4】:
        1. 你需要检查i+1是否小于textLength(需要在每个删除语句后重新分配,如2所示)或textSb.length()在你之前 做textSb.deleteCharAt(i+1);

          您可以在现有的if 条件本身中添加检查:

               if (textSb.charAt(i) == ' ' && i+1<textLength)
          

               if (textSb.charAt(i) == ' ' && i+1<textSb.length())
          

        1. 删除时还需要重新赋值textLength 像 Elliott 提到的一个角色。

          • 在每个删除语句后添加textLength = textSb.length();

          • for loop 更改为for(int i = 3; i&lt; textSb.length(); i += 4)

        【讨论】:

          猜你喜欢
          • 2017-05-02
          • 1970-01-01
          • 1970-01-01
          • 2013-05-13
          • 1970-01-01
          • 2010-10-31
          • 1970-01-01
          • 2016-09-29
          相关资源
          最近更新 更多