文字列を受け取り、別の文字列のインスタンスをすべて削除するプログラムを作成しようとしています。たとえば、("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あなたは 'replace(" em "、" ")'を使うことはできませんか? –
与えられた答えを見て、あなたを最も援助するものを受け入れる(http://stackoverflow.com/help/accepted-answer)のはどうでしょうか? –