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