Quickly filter string content

Source: Internet
Author: User
Public class stringfilterhelper {private int maxlength; private hashmap <string, string> filterstrs = new hashmap <string, string> (); /***** number of values 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 * to obtain the remaining unfiltered string */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 * to 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 string */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;}/*** filter string * @ Description: Mainly post content * @ method_name: filterstring * @ Param filterrole * @ Param filterstr * @ return: string * @ creation date: Apr 19,201 3 4:10:20 * @ version: v1.00 * @ Author: bluesky * @ update date: * @ update Author: bluesky */public static string filterstring (string filterrole, string filterstr) {stringfilterhelper T = new stringfilterhelper (); string [] S = filterrole. split (","); For (INT I = 0; I <S. length; I ++) {T. put (s [I]);} return T. filter (filterstr);} public static void main (string [] AGRs) {string filterrole = "TMD, tnnd, fuck,"; system. out. println (stringfilterhelper. filterstring (filterrole, "TMD, fuck, tnnd. fuck .. tmdtmdtmdtmd. ttnnd. 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.