Итак, я получаю сообщение об ошибке такого типа:
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--", потому что текст становится меньше с одним символом за раз, что будет означать, что мой индекс не будет работать, поскольку я продолжаю удалять эти символы.
Основная проблема заключается в этой ошибке. Как можно 447 не быть в пределах досягаемости, учитывая, что в моей строке около 597 символов.