In the past two days, I suddenly thought of dirty word filtering and wrote one by myself based on the information I found on the Internet, the number of dirty words is more than 700 (the efficiency is not very high. The test is 3-4 times faster than replace in the case of more than 110 KB)
Unit: seconds
Code
System. text. stringbuilder sb = new system. text. stringbuilder (text. length); string filtertext = "The Dirty characters to be filtered are separated by |"; // you can use the separator string [] filterdata = filtertext as needed. split ('|'); foreach (VAR item in filterdata) {char value = item [0]; If (diclist. containskey (value) diclist [value]. add (item); else diclist. add (value, new list <string> () {item}) ;}int COUNT = text. length; For (INT I = 0; I <count; I ++) {char word = text [I]; If (diclist. containskey (Word) // if this key exists in the dictionary table {int num = 0; // whether matching keyword 1 found 0 not found var DATA = diclist [word]. orderby (G => G. length); // sort the dictionary set of the key by the number of characters (which makes it easy to search for the string from less to more) foreach (VAR wordbook in data) {if (I + wordbook. length <= count) // if the index of the string to be truncated is smaller than the total length, the string result is intercepted {string result = text. substring (I, wordbook. length); // compare the number of characters after the keyword length. If (result = wordbook) {num = 1; sb. append (getstring (result); I = I + wordbook. length-1; // The index break of I is successfully changed at the same time; }}if (num = 0) Sb. append (Word);} else sb. append (Word);} return sb. tostring ();} /// <summary> /// Replace the asterisks /// </Summary> /// <Param name = "value"> </param> /// <returns> </returns> Private Static string getstring (string value) {string starnum = string. empty; For (INT I = 0; I <value. length; I ++) {starnum + = "*";} return starnum ;}
Other areas to be optimized
If you have better methods, please share with us some instructions on the principles.