每次出现此子字符串时,我都想从字符串缓冲区中删除一个子字符串,到目前为止我所做的是:
public static void main(String[] args) {
String text = "Alice Bob Alice Bob Alice Bob";
String substr = "Alice";
StringBuffer strbuf = new StringBuffer(text);
strbuf.indexOf(substr);
strbuf.lastIndexOf(substr);
while (strbuf != null) {
strbuf.delete(strbuf.indexOf(substr), strbuf.indexOf(substr) + substr.length());
System.out.println(strbuf.toString());
}
}
我想删除每次出现的“爱丽丝”。但它给出了以下错误:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
我认为是 while 行中的错误。有任何想法吗?