例1 /// <summary> /// 過濾關鍵字 /// </summary> /// <param name="value">過濾字眼,用| 隔開</param> /// <param name="text">傳入要過濾的資料</param> /// <returns></returns> public List<string> FriterText(string value, string text) { List<string> FriterList = new List<string>(); String regex = string.Format("({0})", value); //String regex = "(插你|企業|企業家)"; Regex rg = new Regex(regex); if (rg.IsMatch(text)) { MatchCollection mc = rg.Matches(text); foreach (var item in mc) { if (!FriterList.Contains(item.ToString())) FriterList.Add(item.ToString()); } } return FriterList; } 例1 class Program { static void Main(string[] args) { string keys = "雲風部落格|並發問題|最爛的方法|記憶體|集合|資料庫|效能";
using (new OperationTimer("keyWords:")) { WordSearch ws = new WordSearch(keys); string str = "看雲風部落格關於解決12306並發問題的啟發:我現在做駿卡介面,可能出現並發問題,就是一個訂單可能向我們的介面發送多個請求,而我現在做的方法是去資料庫中對應的表驗證,看訂單是否存在,如果存在就提示一下,如果不存在按流程走,但是這個樣每來一個訂單我都需要去資料庫查,如果我在記憶體中維護一個訂單集合,這樣就能很快解決判斷訂單是否存在的問題,慣性思維太嚴重了,什麼都去資料庫查,這樣的效能是最差的,其實很多問題在記憶體中就可以搞定的,最近還有一個特別感受,不要做井底之蛙,多看牛人的東西收穫真的比自己埋頭寫代碼進步快很多,其實很多時候我寫的程式效能差,效率低都是因為方法的原因,沒有找到好的方法,沒有靈光一閃的感覺,用了最爛的方法解決問題"; ConsoleColor color = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("原文:"); Console.ForegroundColor = color; Console.WriteLine(str); Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("過濾後:"); Console.ForegroundColor = color; Console.WriteLine(ws.Filter(str)); } Console.Read(); } } 例3
using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Collections; namespace BLL.Common { #region 操作類 public class KeywordsFilter { #region 關鍵字過濾 /// <summary> /// 關鍵字過濾 /// /// </summary> /// <param name="keywords"></param> /// <returns></returns> public static string Filter(string keywords) { //需過濾關鍵字集合 List<string> badwords = new List<string>(); KeywordsFilterClass kf = new KeywordsFilterClass(); keywords = kf.BadwordInKeywords(keywords, badwords); return keywords; } #endregion } #endregion #region 關鍵字過濾類 /// <summary> /// 關鍵字過濾類 /// </summary> public class KeywordsFilterClass { private Dictionary<string, object> hash = new Dictionary<string, object>(); //髒字字典 開頭髒字儲存 private BitArray firstCharCheck = new BitArray(char.MaxValue); //髒字字典 單個char儲存 private BitArray allCharCheck = new BitArray(char.MaxValue); private int maxLength = 0; /// <summary> /// 初始化 已儲存的 過濾字串 /// </summary> /// <param name="words"></param> private void InitHash(List<string> badwords) { foreach (string word in badwords) { //儲存字典內不存在的髒字 if (!hash.ContainsKey(word)) { hash.Add(word, null); //設定髒字計算長度 this.maxLength = Math.Max(this.maxLength, word.Length); firstCharCheck[word[0]] = true; foreach (char c in word) { allCharCheck[c] = true; } } } } /// <summary> /// 替換字串中的髒字為指定的字元 /// </summary> /// <param name="text"></param> /// <returns></returns> public string BadwordInKeywords(string text, List<string> badwords) { //初始化 髒字字典 this.InitHash(badwords); int index = 0; while (index < text.Length) { //判斷開頭髒字 if (!firstCharCheck[text[index]]) { //未找到開頭髒字 則索引累加 while (index < text.Length - 1 && !firstCharCheck[text[++index]]) ; } for (int j = 1; j <= Math.Min(maxLength, text.Length - index); j++) { if (!allCharCheck[text[index + j - 1]]) { break; } string sub = text.Substring(index, j); if (hash.ContainsKey(sub)) { text = text.Replace(sub, "**"); //this.InitHash(badwords); index += j; break; } } index++; } return text; } } #endregion }
|