public class StringDetectionUtil {
/**
* 检测并替换字符串 用*代替
* @param content 需要检测的文本
* @param badString 脏话 关键字 对比的字符串数组
* @return
*/
public static String detection(String content,String[] badString){
try {
for (int i = 0; i <badString.length ; i++) {
content= index(content,badString[i]);
}
return content;
}catch (Exception e){
Log.e("StringDetectionUtil",e.getMessage());
return "";
}
}
private static String index(String content,String badString){
if(content.equals("")||badString.equals("")){
return content;
}
int index=content.indexOf(badString);
String newString="";
if(index!=-1){
String newString1=content.substring(0,index);
String newString2=content.substring(index+badString.length());
String hindString="";
for (int i = 0; i <badString.length() ; i++) {
hindString=hindString+"*";
}
newString=newString1+hindString+newString2;
return index(newString,badString);
}
return content;
}
}
网友评论