我正在嘗試編寫一個程序,該程序需要一個字符串並從中刪除所有其他字符串的實例。例如:("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)最能幫助你的答案? –