【发布时间】:2021-12-01 13:49:32
【问题描述】:
我有这个:
private String remove_spaces(String s){
s = s.trim();
String updated = "";
for (int i = 0; i < s.length(); i++) {
String tester = s.substring(i, i + 1);
String space = " ";
boolean isSpace = tester.equals(space);
if (isSpace = false)
updated += tester;
}
return updated;
}
它会抛出一个StringIndexOutOfBounds,我不明白为什么。为什么?
【问题讨论】:
-
在 Java 中,您可以在此处使用 replaceAll 删除空格 stackoverflow.com/questions/5455794/…
-
@ДөлгөөнМөнхчулуун 是的,但是指定了 using substring
标签: java substring indexoutofboundsexception removing-whitespace