如原句:“窝草,昨晚又停电了!”
过滤后为:“**,昨晚又停电了!”
分析:
1,可以用数组事先存储多个指定的敏感单词;
2,针对所有的敏感字都需要判断及过滤;
3,敏感字替换;replace(), replaceAll()
4,替换的*字符和原字符长度相同;
代码如下:
public class ReplaceDemo {
//敏感词替换**方法
public static String test(int sum) {
String x = ""; //定义空格字符
//遍历传过来的敏感词数量
for (int i = 0; i < sum; i++) {
x +="*"; //敏感词换成**
} return x;
}
public static void main(String[] args) {
String[] world = {"我草","SB","扑街"};//敏感词库
String text = " 我 草 ,昨晚又停电了SB,真扑街";//文本
//去空格
String replaceText = text.replaceAll(" ", "");
//遍历数组
for (String s : world) {
if (replaceText.contains(s)) {
//替换后输出
replaceText = replaceText.replaceAll(s, test(s.length()));
}
}
System.out.println(replaceText);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)