Java string filter method implementation

Source: Internet
Author: User

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 "));

}

}

 

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.