问题:输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String [] strings = new String[2];
String first = "";
String second = "";
for(int i = 0;i < 2;i++) {
strings[i] = scanner.nextLine();
}
first = strings[0];
second = strings[1];
outputString(first,second);
}
public static void outputString(String first,String second){
for(int i = 0;i < second.length();i ++){
String temp = String.valueOf(second.charAt(i));
first = first.replaceAll(temp,"");
}
System.out.println(first);
}
}
要注意的就一点,String.replaceAll()是有返回值的,并不是将值直接赋给原字符串