Source: xingd.net-blog
But in my tests, RegEx is about twice faster. However, I am still not satisfied. I should use a lot of dirty words for filtering on our website, which has some impact on efficiency. After some consideration, I made Algorithm . I tested it on my machine and used the dirty Character Library in the original text, the string length of 0x19c, 1000 loops, 1933.47 ms for text search, and 1216.719 ms for RegEx, my algorithm only uses 244.125 Ms.
The main algorithms are as follows:CodeAs shown in
Private Static dictionary DIC = New Dictionary ();
Private Static bitarray fastcheck = new bitarray (char. maxvalue );
Static void prepare ()
{
String [] badwords = // read from File
Foreach (string word in badwords)
{
If (! Dic. containskey (Word ))
{
Dic. Add (word, null );
Maxlength = math. Max (maxlength, word. Length );
Int value = word [0];
Fastcheck [word [0] = true;
}
}
}
when using
int Index = 0;
while (index {< br> If (! Fastcheck [target [Index])
{< br> while (index }< br> for (Int J = 0; j {< br> string sub = target. substring (index, J);
If (DIC. containskey (sub)
{< br> Sb. replace (sub, "***", index, J);
index + = J;
break;
}< BR >}< br> index ++;
}