java如何判断字符串中有中文

在Java中有个Character.UnicodeBlock静态类,Character.UnicodeBlock.of(char c)返回该字符的文字常量。
例如:
class Test{
//判断该字符是否为中文
private boolean isChinese(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.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
}

/*判断该字符串中是否有中文*/
private boolean isChinese(String strName) {
        char[] ch = strName.toCharArray();
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (isChinese(c) == true) {
                return true;
            }
        }
        return false;
}
public static void main(String[] args) {
       isChinese("你hao")
}
}

注:判断其它语系请参考Java帮助文档中Character.UnicodeBlock静态类

你可能感兴趣的:(java,C++,c,C#)