Java Perfect judgment Chinese characters

Source: Internet
Author: User

Java determines whether a string is in Chinese the general situation is to use Unicode encoding (CJK Unified Chinese Character Coding interval: 0X4E00–0X9FBB) of the regular to make judgments, but in fact this interval to judge Chinese is not very accurate, because some Chinese punctuation such as:,. The wait is not recognized.

The following is a more comprehensive method of judging: Charutil.java

http://www.micmiu.com/lang/java/java-check-chinese/

import java.util.regex.Pattern;public class CharUtil {    publicstatic void main(String[] args) {        String[] strArr =new String[] { "www.micmiu.com", "[email protected]#$%^&*()_+{}[]|\"‘?/:;<>,.","!¥……()——:;“”‘’《》,。?、","不要啊", "やめて","韩佳人", "???"};        for(String str : strArr) {            System.out.println("===========> 测试字符串:"+ str);            System.out.println("正则判断结果:"+ isChineseByREG(str) + " -- "+ isChineseByName(str));            System.out.println("Unicode判断结果 :"+ isChinese(str));            System.out.println("详细判断列表:");            char[] ch = str.toCharArray();            for(int i = 0; i < ch.length; i++) {                charc = ch[i];                System.out.println(c +" --> " + (isChinese(c) ?"是" : "否"));            }        }    }    // 根据Unicode编码完美的判断中文汉字和符号    privatestatic booleanisChinese(char c) {        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);        if(ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {            returntrue;        }        returnfalse;    }    // 完整的判断中文汉字和符号    publicstatic booleanisChinese(String strName) {        char[] ch = strName.toCharArray();        for(int i = 0; i < ch.length; i++) {            charc = ch[i];            if(isChinese(c)) {                returntrue;            }        }        returnfalse;    }     // 只能判断部分CJK字符(CJK统一汉字)    publicstatic booleanisChineseByREG(String str) {        if(str == null) {            returnfalse;        }        Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");        returnpattern.matcher(str.trim()).find();    }    // 只能判断部分CJK字符(CJK统一汉字)    publicstatic booleanisChineseByName(String str) {        if(str == null) {            returnfalse;        }        // 大小写不同:\\p 表示包含,\\P 表示不包含        // \\p{Cn} 的意思为 Unicode 中未被定义字符的编码,\\P{Cn} 就表示 Unicode中已经被定义字符的编码        String reg ="\\p{InCJK Unified Ideographs}&&\\P{Cn}";        Pattern pattern = Pattern.compile(reg);        returnpattern.matcher(str.trim()).find();    }}

Java Perfect judgment Chinese characters

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.