java-determines if the string contains Chinese and filters out Chinese

Source: Internet
Author: User

createtime--2017 September 6 08:48:59

Author:marydon

Java determines if the string contains Chinese and filters out Chinese

1. Determine if the string contains Chinese method encapsulation

/*** Determine if the string contains Chinese *@paramstr * Pending Check String *@returnIs Chinese * @warn cannot verify whether it is a Chinese punctuation mark*/ Public Static BooleanIscontainchinese (String str) {Pattern P= Pattern.compile ("[\u4e00-\u9fa5]"); Matcher m=P.matcher (str); if(M.find ()) {return true; }    return false;}

Description: Import Required

Import Java.util.regex.Matcher; import Java.util.regex.Pattern;

2. Filter Chinese

/*** Filter out Chinese *@paramStr string to filter Chinese *@returnfilter out Chinese after string*/ Public Staticstring Filterchinese (String str) {//used to return resultsString result =str; BooleanFlag =Iscontainchinese (str); if(flag) {//includes Chinese//used to splice the characters after filtering ChineseStringBuffer SB =NewStringBuffer (); //used to verify whether the Chinese        BooleanFlag2 =false; //for temporary storage of single characters        CharChinese = 0; //5. Remove the Chinese in the file name//Convert a string to char[]        Char[] Chararray =Str.tochararray (); //filter to Chinese characters         for(inti = 0; i < chararray.length; i++) {Chinese=Chararray[i]; Flag2=Ischinese (Chinese); if(!FLAG2) {//not CJK characters and punctuation marks.Sb.append (Chinese); }} result=sb.tostring (); }    returnresult;}

Description:Ischinese (char) method see article: java-Chinese and special character check

3. Testing

 Public Static void Main (string[] args) {    = "test, Chinese";    System.out.println (Filterchinese (FileName));}

4. More verification Please visit the article: java-Chinese and special character check

java-determines if the string contains Chinese and filters out Chinese

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.