编写敏感词过滤程序
说明:在网络程序中,如聊天室、聊天软件等,经常需要对一些用户所提交的聊天内容中的敏感性词语进行过滤。
如"性"、“色情”、“爆炸”、“恐怖”、“枪”、"军火"等,这些都不可以在网上进行传播,需要过滤掉或者用其他词语替换掉。
思路:将用户的聊天内容保存到一个字符串对象或一个StringBuilder对象中,
然后与敏感词语进行比对。如果属于敏感词语,就过滤掉或替换掉。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("输入内容,进行过滤:");
String s = sc.next() ;
exchange(s);
}
public static void exchange(String s) {
String [] str = {"性","色情","爆炸","恐怖","枪","军火"} ;
for(int i=0;i<str.length;i++)
if(s.contains(str[i]))
s=s.replace(str[i], "***");
System.out.println(s);
}
}