C # Dirty word Filtering

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.