我正在尝试编写一个程序,该程序需要一个字符串并从中删除所有其他字符串的实例。例如:("Remove them all!", "em")
将打印"Rove th all!"
。但是,当我运行这个,它给了我java.lang.StringIndexOutOfBoundsException
。为什么我继续接收java.lang.StringIndexOutOfBoundsException
public class LabFive {
public static String removeAll(String oldPhrase, String removal){
String newPhrase = "";
for(int i = 0; i <= oldPhrase.length(); i++){
if(oldPhrase.substring(i, (removal.length() + i)) == removal)
newPhrase = newPhrase + oldPhrase.substring((removal.length() + 2 + i), oldPhrase.length());
}
return(newPhrase);
}
public static void main(String[] args) {
System.out.println(removeAll("AaAaAa", "a"));
}
}
W¯¯ hy你不能用'replace(“em”,“”)'? –
你为什么不看看给出的答案,或许[接受](http://stackoverflow.com/help/accepted-answer)最能帮助你的答案? –