字符串精練——編寫敏感詞過濾程序

編寫敏感詞過濾程序
說明:在網絡程序中,如聊天室、聊天軟件等,經常需要對一些用戶所提交的聊天內容中的敏感性詞語進行過濾。
如"性"、“色情”、“爆炸”、“恐怖”、“槍”、"軍火"等,這些都不可以在網上進行傳播,需要過濾掉或者用其他詞語替換掉。
思路:將用戶的聊天內容保存到一個字符串對象或一個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);
		}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章