我是一个试图解决问题的学生.

我需要创建一个方法来从另一个字符串中删除在指定字符串中找到的所有字符.

因此,如果String str =“ hello world”和String remove =“ eo”

该方法将返回“ hll wrld”.

我的解决方案设置方式将结果字符串打印出来很多次,这是我不希望的.有没有简单的解决方法,还是我需要重新设计方法?

class StringStuff{
public static void main (String [] args){
String str = "This is a string that needs to be changed";
String remove = "iaoe";
System.out.println(removeChars(str, remove));
}
public static String removeChars(String str, String remove){
String newStr = "";
for(int i=0;i
for(int j=0; j
if(str.charAt(j)!=remove.charAt(i)){
newStr = newStr+str.charAt(j);
}
}
}
return newStr;
}
}

更新

感谢您的答复,我发现了另一种受您提供的解决方案启发的“新手”方式.

public static String removeChar(String str, String remove){
String newStr = "";
boolean match = false;
for(int i = 0; i
for(int j=0; j
if(str.charAt(i) == remove.charAt(j))
match = true;
}
if(match == false)
newStr = newStr + str.charAt(i);
match = false;
}
return newStr;
}