import java.util.Scanner;
public class ShieldDirtyTalk {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("你对他说:");
String s = sc.nextLine();//键盘录入要说的话。
StringBuffer sb = new StringBuffer(s);
String[] arr = { "caonima", "caonimei", "草你妈", "草你妹", "草你大爷", "王八蛋",
"狗日的" };//自己自定义的一部分需要进行屏蔽的脏话,可以根据需要添加。
for (int i = 0; i < arr.length; i++) {
int start = sb.indexOf(arr[i]);
int end = sb.indexOf(arr[i]) + arr[i].length();
if (start != -1) {
//利用字符串缓冲区的替换功能,用*号替换掉脏话
sb.replace(start, end, getAsterisk(arr[i]));
}
if (start != -1) {
i--;
}
}
System.out.println(sb);//输出经过替换处理的字符串
}
//定义了一个方法,使*号长度与脏话字符串长度一致
public static String getAsterisk(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
sb.append("*");
}
String s1 = sb.toString();
return s1;
}
} |
|