Example 1 <summary> Filter keywords </summary> <param name= "Value" > Filter words, with | Separate </param> <param name= "text" > Incoming data to be filtered </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 = "(Insert you | enterprise | entrepreneur)"; 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; } Example 1 Class Program { static void Main (string[] args) { string keys = "Cloud Wind Blog | concurrency problem | worst Way | memory | collection | database | performance"; using (New Operationtimer ("KeyWords:")) { Wordsearch ws = new Wordsearch (keys); String str = "See Cloud Wind blog about solving 12306 concurrent problems: I now do the June card interface, there may be a concurrency problem, that is, an order may send multiple requests to our interface, and I now do is to go to the database in the corresponding table validation, to see whether the order exists, if there is a hint , if there is no flow by process, but this sample every order I need to go to the database to check, if I maintain a set of orders in memory, so that can quickly solve the problem of judging the existence of orders, inertia thinking is too serious, what all go to the database to check, such performance is the worst, In fact, a lot of problems in memory can be done, and recently there is a special feeling, do not do a frog in the well, look at the cattle of things to harvest really more than their own buried writing code progress much faster, in fact, many times I write the program performance is poor, inefficient are due to the method of reason, did not find a good method, no flash of the feeling, Use the worst way to solve the problem "; Consolecolor color = Console.foregroundcolor; Console.foregroundcolor = consolecolor.red; Console.WriteLine ("Original:"); Console.foregroundcolor = color; Console.WriteLine (str); Console.WriteLine (); Console.foregroundcolor = consolecolor.red; Console.WriteLine ("Filtered:"); Console.foregroundcolor = color; Console.WriteLine (ws. Filter (str)); } Console.read (); } } Example 3 Using System; Using System.Collections.Generic; Using System.Text; Using System.Data; Using System.Collections; Namespace Bll.common { #region Action Class public class Keywordsfilter {
#region keyword Filter <summary> Keyword filtering /// </summary> <param name= "keywords" ></param> <returns></returns> public static string Filter (String keywords) {
Need to filter the keyword collection list<string> badwords = new list<string> ();
Keywordsfilterclass KF = new Keywordsfilterclass (); Keywords = KF. Badwordinkeywords (keywords, badwords); return keywords; } #endregion } #endregion #region Keyword Filter class <summary> Keyword Filter class </summary> public class Keywordsfilterclass { Private dictionary<string, object> hash = new dictionary<string, object> (); Dirty Word dictionary opening dirty Word store Private BitArray Firstcharcheck = new BitArray (char. MaxValue); Dirty Word dictionary single char store Private BitArray Allcharcheck = new BitArray (char. MaxValue); private int maxLength = 0; <summary> Initialize the stored filter string </summary> <param name= "Words" ></param> private void Inithash (list<string> badwords) { foreach (string word in badwords) { Save a dirty word that doesn't exist in the dictionary if (!hash. ContainsKey (word)) { Hash. ADD (word, NULL); Set a dirty word calculation length This.maxlength = Math.max (This.maxlength, Word. Length); Firstcharcheck[word[0]] = true; foreach (char c in Word) { Allcharcheck[c] = true; } } } } <summary> The dirty word in the replacement string is the specified character </summary> <param name= "Text" ></param> <returns></returns> public string Badwordinkeywords (string text, list<string> badwords) { Initialize a dirty word dictionary This. Inithash (badwords); int index = 0; while (Index < text. Length) { To judge the beginning of a dirty word if (!firstcharcheck[text[index]]) { Index accumulates without the beginning dirty word found 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 } |