Public class DirtyWordOper
{
Private static Dictionary <string, object> hash = new Dictionary <string, object> ();
Private static BitArray firstCharCheck = new BitArray (char. MaxValue); // record the first character of a dirty word
Private static BitArray allCharCheck = new BitArray (char. MaxValue); // records all the characters of each dirty word
Private static int maxLength = 0 ;//
Private static bool onlyOne = true;
# Region
/// <Summary>
/// Return the length of the replaced string.
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Public string Replace (string text)
{
If (onlyOne)
{
Init (); // initialization data will not be executed once
OnlyOne = false;
}
If (! IsDirtyword (text ))
{
Return text;
}
// Obtain the replacement operation table
List <DetailRepModel> drlist = GetList (text );
// Perform the Replacement Operation
Return Replace2 (text, drlist );
}
/// <Summary>
/// Perform initialization only once
/// </Summary>
/// <Param name = "text"> </param>
Private static void Init ()
{
String [] badwords = DirtyWordData. DirtyKeyword. Split (| );
Foreach (string bw in badwords)
{
String [] strarrtemp = bw. Split (&);
String word = strarrtemp [0];
Word = word. Trim (); // remove spaces and format symbols from the data.
Word = word. Replace ("/r ","");
Word = word. Replace ("/n ","");
If (word = "")
{
Break;
}
If (! Hash. ContainsKey (word ))
{
Hash. Add (word, null );
MaxLength = Math. Max (maxLength, word. Length );
FirstCharCheck [word [0] = true;
Foreach (char c in word)
{
AllCharCheck [c] = true;
}
}
}
}
/// <Summary>
/// Whether dirty words are contained
/// </Summary>
/// <Param name = "text"> </param>
/// <Returns> </returns>
Private static bool isDirtyword (string text)
{
Int index = 0;
// Int offset = 0;
While (index <text. Length)
{
// If the first character does not match
If (! FirstCharCheck [text [index])
{// Locate the same as the first character of a dirty word
While (index <text. Length-1 &&! FirstCharCheck [text [++ index]);
}
For (int j = 1; j <= Math. Min (maxLength, text. Length-index); j ++)
{
& Nb