/**
* 字符串 + 字符串数组实现脏数据过滤: eg: 这个商品MMP 就是 垃圾 NND 。。
*/
public class Task_05 {
public static void main(String[] args) {
String str[] = {"垃圾", "mmp", "nnp", "独裁" };
String st = "那个啥Mmp,能及时撒地pMH方垃圾,是地方MMP教会独裁,Nnp撒地Mp撒地方!!";
// 第一种方法:缺点(把所有的字母都转成小写字母了)
/*
* for (int i = 0; i < str.length; i++) { st=st.toLowerCase();
* st=st.replace(str[i], "***"); }
*
*/
// 第二种:解决上面问题
int num = str[0].length();
for (int j = 0; j < st.length() - num; j++) {
for (int i = 0; i < str.length; i++) {
String s = "";
num = str[i].length();
for (int k = 0; k < num; k++) {
s += st.charAt(j + k);
}
if (str[i].equalsIgnoreCase(s)) {
st = st.replace(s, "***");
}
}
}
System.out.println(st);
}
}
字符串 + 字符串数组实现脏数据过滤
最新推荐文章于 2022-10-22 18:13:03 发布