Java uses regular expressions to remove English from strings

Source: Internet
Author: User
Tags stringbuffer

Use regular expressions to remove English from a string

String str = "111,AAA,222,BBB"; Pattern p = pattern.compile ("[a-za-z]");//Create Stringbuff to splice stringbuffer sb = new StringBuffer (); string[] s = str. Split (","); for (int i = 0; i < s.length; i++) {    //If there are English letters I do not handle this side, if there is a need, you can add the If    (P.matcher (s) [i]). Find ()) {    //does not contain the English alphabet    }else{        //is stitched into a new string, judging if the current index is not the last one, add "," if        (I < s.length-1) {            After stitching up the string) Sb.append (S[i] + ",");        } else {            //If it is the last one, add            sb.append (S[i]) directly        ;        }}} Verify that the last digit contains ","  this occurs because the last character contains the English, I did not handle, the penultimate character in the concatenation, because not the last, so will add a ","        String SBS = Sb.tostring ();        if (Sbs.endswith (",")) {            SBS = sbs.substring (0,sbs.length ()-1);        }        SYSTEM.OUT.PRINTLN (SBS);

Output Result:

111,222
Related Article

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.