In this way, the system filters out: "You TMD, too wicked." After filtering, "You are ***, too wicked." filter the swearing language into the * number.
Package test;
Import java. util. hashmap;
/**
* Maxlength-the length of the longest string to be filtered
* Filterstrs <string, string>-a collection of strings to be filtered. The key is the string to be filtered, and the value is the string to be filtered, for example "*"
* @ Author Administrator
*
*/
Public class test {
Private int maxlength;
Private hashmap <string, string> filterstrs = new hashmap <string, string> ();
/**
* Quantity to be filtered out during initialization *
*/
Private string initstr (int n ){
Stringbuffer sb = new stringbuffer ();
For (INT I = 0; I <n; I ++ ){
SB. append ('*');
}
Return sb. tostring ();
}
/**
* Str-filtered string
* S-string to be filtered
* Obtain the remaining unfiltered strings.
*/
Private string getnextstr (string STR, int start, int slength ){
If (START = 0 ){
STR = Str. substring (slength );
} Else if (start + slength <Str. Length ()){
STR = Str. substring (start + slength );
}
Return STR;
}
/**
* Str-filtered string
* S-string to be filtered
* Obtain the filtered string
*/
Private stringbuffer getfilterstr (stringbuffer Sb, string STR, int start, string s ){
If (start! = 0 ){
SB. append (Str. substring (0, start ));
}
SB. append (filterstrs. Get (s ));
Return Sb;
}
/**
* Str-filtered string
* Filter and combine the filtered strings
*/
Public String filter (string Str ){
Stringbuffer resultstr = new stringbuffer ();
For (INT start = 0; Start <Str. Length (); Start ++ ){
For (INT end = start + 1; end <= Str. Length () & End <= start + maxlength; end ++ ){
String S = Str. substring (START, end );
Int slength = S. Length ();
If (filterstrs. containskey (s )){
Resultstr = getfilterstr (resultstr, STR, start, S );
STR = getnextstr (STR, start, slength );
Start = 0;
End = start;
}
}
}
Resultstr. append (STR );
Return resultstr. tostring ();
}
Public void put (string key ){
Int keylength = key. Length ();
Filterstrs. Put (Key, initstr (keylength ));
If (keylength> This. maxlength)
Maxlength = keylength;
}
Public static void main (string [] AGRs ){
Test T = new test ();
T. Put ("TMD ");
T. Put ("tnnd ");
T. Put ("operation ");
T. Put ("nnd ");
System. Out. println (T. Filter ("TMD, fuck, tnnd. Fuck... tmdtmdtmdtmdtmd. ttnnd. Fuck tmdtnndtnndtnnd "));
}
}