package com.jsoft.test;

import java.util.regex.Pattern;

/**
* 判断中文字符
*
* @author jim
* @date 2017-12-22
*/
public class ChineseHelper {
public static void main(String[] args) {
// 纯英文
String s1 = "Hello,Tom.!@#$%^&*()_+-={}|[];':\"?";
// 纯中文(不含中文标点)
String s2 = "你好中国";
// 纯中文(含中文标点)
String s3 = "你好,中国。《》:“”‘';()【】!¥、";
// 韩文
String s4 = "한국어난";
// 日文
String s5 = "ぎじゅつ";
// 特殊字符
String s6 = "��";
String s7 = "╃";
String s8 = "╂";
// 繁体中文
String s9 = "蒼老師";
// 1 使用字符范围判断
System.out.println("s1是否包含中文:" + hasChineseByRange(s1));// false
System.out.println("s2是否包含中文:" + hasChineseByRange(s2));// true
System.out.println("s3是否包含中文:" + hasChineseByRange(s3));// true
System.out.println("s4是否包含中文:" + hasChineseByRange(s4));// false
System.out.println("s5是否包含中文:" + hasChineseByRange(s5));// false
System.out.println("s6是否包含中文:" + hasChineseByRange(s6));// false
System.out.println("s7是否包含中文:" + hasChineseByRange(s7));// false
System.out.println("s8是否包含中文:" + hasChineseByRange(s8));// false
System.out.println("s9是否包含中文:" + hasChineseByRange(s9));// true
System.out.println("-------分割线-------");
System.out.println("s1是否全是中文:" + isChineseByRange(s1));// false
System.out.println("s2是否全是中文:" + isChineseByRange(s2));// true
System.out.println("s3是否全是中文:" + isChineseByRange(s3));// false 中文标点不在范围内
System.out.println("s4是否全是中文:" + isChineseByRange(s4));// false
System.out.println("s5是否全是中文:" + isChineseByRange(s5));// false
System.out.println("s6是否全是中文:" + isChineseByRange(s6));// false
System.out.println("s7是否全是中文:" + isChineseByRange(s7));// false
System.out.println("s8是否全是中文:" + isChineseByRange(s8));// false
System.out.println("s9是否全是中文:" + isChineseByRange(s9));// true
System.out.println("-------分割线-------");
// 2 使用字符范围正则判断(结果同1)
System.out.println("s1是否包含中文:" + hasChineseByReg(s1));// false
System.out.println("s2是否包含中文:" + hasChineseByReg(s2));// true
System.out.println("s3是否包含中文:" + hasChineseByReg(s3));// true
System.out.println("s4是否包含中文:" + hasChineseByReg(s4));// false
System.out.println("s5是否包含中文:" + hasChineseByReg(s5));// false
System.out.println("s6是否包含中文:" + hasChineseByReg(s6));// false
System.out.println("s7是否包含中文:" + hasChineseByReg(s7));// false
System.out.println("s8是否包含中文:" + hasChineseByReg(s8));// false
System.out.println("s9是否包含中文:" + hasChineseByReg(s9));// true
System.out.println("-------分割线-------");
System.out.println("s1是否全是中文:" + isChineseByReg(s1));// false
System.out.println("s2是否全是中文:" + isChineseByReg(s2));// true
System.out.println("s3是否全是中文:" + isChineseByReg(s3));// false 中文标点不在范围内
System.out.println("s4是否全是中文:" + isChineseByReg(s4));// false
System.out.println("s5是否全是中文:" + isChineseByReg(s5));// false
System.out.println("s6是否全是中文:" + isChineseByReg(s6));// false
System.out.println("s7是否全是中文:" + isChineseByReg(s7));// false
System.out.println("s8是否全是中文:" + isChineseByReg(s8));// false
System.out.println("s9是否全是中文:" + isChineseByReg(s9));// true
System.out.println("-------分割线-------");
// 3 使用CJK字符集判断
System.out.println("s1是否包含中文:" + hasChinese(s1));// false
System.out.println("s2是否包含中文:" + hasChinese(s2));// true
System.out.println("s3是否包含中文:" + hasChinese(s3));// true
System.out.println("s4是否包含中文:" + hasChinese(s4));// false
System.out.println("s5是否包含中文:" + hasChinese(s5));// false
System.out.println("s6是否包含中文:" + hasChinese(s6));// false
System.out.println("s7是否包含中文:" + hasChinese(s7));// false
System.out.println("s8是否包含中文:" + hasChinese(s8));// false
System.out.println("s9是否包含中文:" + hasChinese(s9));// true
System.out.println("-------分割线-------");
System.out.println("s1是否全是中文:" + isChinese(s1));// false
System.out.println("s2是否全是中文:" + isChinese(s2));// true
System.out.println("s3是否全是中文:" + isChinese(s3));// true 中文标点也被包含进来
System.out.println("s4是否全是中文:" + isChinese(s4));// false
System.out.println("s5是否全是中文:" + isChinese(s5));// false
System.out.println("s6是否全是中文:" + isChinese(s6));// false
System.out.println("s7是否全是中文:" + isChinese(s7));// false
System.out.println("s8是否全是中文:" + isChinese(s8));// false
System.out.println("s9是否全是中文:" + isChinese(s9));// true
} /**
* 是否包含中文字符<br>
* 包含中文标点符号<br>
*
* @param str
* @return
*/
public static boolean hasChinese(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (isChinese(c)) {
return true;
}
}
return false;
} /**
* 是否全是中文字符<br>
* 包含中文标点符号<br>
*
* @param str
* @return
*/
public static boolean isChinese(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (!isChinese(c)) {
return false;
}
}
return true;
} /**
* 是否是中文字符<br>
* 包含中文标点符号<br>
*
* @param c
* @return
*/
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_C) {
return true;
} else if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D) {
return true;
} else if (ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
} else if (ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
} /**
* 是否包含汉字<br>
* 根据汉字编码范围进行判断<br>
* CJK统一汉字(不包含中文的,。《》()“‘'”、!¥等符号)<br>
*
* @param str
* @return
*/
public static boolean hasChineseByReg(String str) {
if (str == null) {
return false;
}
Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
return pattern.matcher(str).find();
} /**
* 是否全是汉字<br>
* 根据汉字编码范围进行判断<br>
* CJK统一汉字(不包含中文的,。《》()“‘'”、!¥等符号)<br>
*
* @param str
* @return
*/
public static boolean isChineseByReg(String str) {
if (str == null) {
return false;
}
Pattern pattern = Pattern.compile("[\\u4E00-\\u9FBF]+");
return pattern.matcher(str).matches();
} /**
* 是否包含汉字<br>
* 根据汉字编码范围进行判断<br>
* CJK统一汉字(不包含中文的,。《》()“‘'”、!¥等符号)<br>
*
* @param str
* @return
*/
public static boolean hasChineseByRange(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (c >= 0x4E00 && c <= 0x9FBF) {
return true;
}
}
return false;
} /**
* 是否全是汉字<br>
* 根据汉字编码范围进行判断<br>
* CJK统一汉字(不包含中文的,。《》()“‘'”、!¥等符号)<br>
*
* @param str
* @return
*/
public static boolean isChineseByRange(String str) {
if (str == null) {
return false;
}
char[] ch = str.toCharArray();
for (char c : ch) {
if (c < 0x4E00 || c > 0x9FBF) {
return false;
}
}
return true;
}
}

如果仅仅去判断是否是中文,不需判断中文标点的话,推荐使用正则去匹配,可能更高效点。

还有另外一种投机取巧的方法:转int类型,然后try...catch

参考:

http://www.jb51.net/article/79101.htm(以上内容转自此篇文章)

http://blog.csdn.net/h082602/article/details/73251446

http://blog.csdn.net/u011240877/article/details/49907751

http://blog.csdn.net/l1028386804/article/details/43764073

http://blog.csdn.net/qwkxq/article/details/53508736

https://www.cnblogs.com/jinc/archive/2013/02/26/2933766.html

Java判断中文字符的更多相关文章

  1. Java 判断中文字符

    Java判断一个字符串中是否有中文字符有两种方法,但是原理都一样,就是通过Unicode编码来判断,因为中文在Unicode中的编码区间为:0x4e00--0x9fa5 第一种: String chi ...

  2. Java判断一个字符是否是数字的几种方法的代码

    在工作期间,将写内容过程经常用到的一些内容段做个记录,下面内容是关于Java判断一个字符是否是数字的几种方法的内容,希望能对码农们有好处. public class Test{ public stat ...

  3. JAVA的中文字符乱码问题

    来源:http://luzefengoo.blog.163.com/blog/static/1403593882012754428536/ JAVA的中文字符乱码问题一直很让人头疼.特别是在WEB应用 ...

  4. Java 完美判断中文字符

    Java判断一个字符串是否有中文一般情况是利用Unicode编码(CJK统一汉字的编码区间:0x4e00–0x9fbb)的正则来做判断,但是其实这个区间来判断中文不是非常精确,因为有些中文的标点符号比 ...

  5. Java 完美判断中文字符的方法

    Java判断一个字符串是否有中文一般情况是利用Unicode编码(CJK统一汉字的编码区间:0x4e00–0x9fbb)的正则来做判断,但是其实这个区间来判断中文不是非常精确,因为有些中文的标点符号比 ...

  6. C# 判断中文字符(字符串)

    在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs.通过对字符的unicode编码进行判断来确定字符是否为中文.protected bool  ...

  7. python利用utf-8编码判断中文字符

    下面这个小工具包含了 判断unicode是否是汉字,数字,英文,或者其他字符. 全角符号转半角符号. unicode字符串归一化等工作. 还有一个能处理多音字的汉字转拼音的程序,还在整理中. #!/u ...

  8. MySQL判断中文字符的方法(转)

    准备: 2.1.环境 MySQL mysql> SHOW VARIABLES LIKE "%version%"; +-------------------------+--- ...

  9. java 获取中文字符的首字母

    原理: GB2312编码中的中文是按照拼音排序的 注意: 一些生僻的字无法获得正确的首字母,原因是这些字都是后加入的. import java.io.UnsupportedEncodingExcept ...

随机推荐

  1. 动态删边SPFA: [HNOI2014]道路堵塞

    [HNOI2014]道路堵塞 题目描述 $A$ 国有 $N$座城市,依次标为$1$到$N$.同时,在这$N$座城市间有$M$条单向道路,每条道路的长度是一个正整数.现在,$A$国交通部指定了一条从城市 ...

  2. HDU 4758 Walk Through Squares( AC自动机 + 状态压缩DP )

    题意:给你两个串A,B, 问一个串长为M+N且包含A和B且恰好包含M个R的字符串有多少种组合方式,所有字符串中均只含有字符L和R. dp[i][j][k][S]表示串长为i,有j个R,在自动机中的状态 ...

  3. 个人支付宝监控并自动获取交易记录对接系统API

    我们都知道,支付宝支付API接口只有企业才能使用,但有一部分业务,可能我们不方便使用企业收款,但又想做到自动化,那怎么办呢 于是一个支付宝交易记录自动监控软件诞生了. 支付宝都有一个收款二维码,收款提 ...

  4. 【bzoj3866】The Romantic Hero dp

    题目描述 给你n个数,从中选出两个不相交非空集合S和T,使得S中的每一个元素都在T集合的前面,并且S集合中的所有数的亦或等于T集合中的所有数的与,求方案数 mod 10^9+7. 输入 The fir ...

  5. [codeforces934D]A Determined Cleanup

    [codeforces934D]A Determined Cleanup 试题描述 In order to put away old things and welcome a fresh new ye ...

  6. [codeforces438E]The Child and Binary Tree

    [codeforces438E]The Child and Binary Tree 试题描述 Our child likes computer science very much, especiall ...

  7. 【CZY选讲·一道图论好题】

    题目描述 LYK有一张无向图G={V,E},这张无向图有n个点m条边组成.并且这是一张带权图,不仅有边权还有点权.LYK给出了一个子图的定义,一张图G'={V',E'}被称作G的子图,当且仅当: · ...

  8. 每天一个小算法(insertion sort3)

    今天多看看插入排序的理论部分. 先贴几个概念吧: 1.伪代码(英语:pseudocode),又称为虚拟代码,是高层次描述算法的一种方法.它不是一种现实存在的编程语言(已经出现了类似伪代码的语言,参见N ...

  9. server reached pm.max_children setting (5), consider raising it

    先查看日志 /data1/server/php-cgi/var/log/php-fpm.log[19-Dec-2012 11:41:13] WARNING: [pool www] server rea ...

  10. 【Tomcat】Tomcat下设置项目为默认项目

    项目的实际使用中经常需要将当前项目设为tomcat的默认项目,而不是进入到tomcat的页面,有几种方法可以实现,注意第二种.第三种情况需要先删除webapps下的ROOT目录,否则会失败. 一. 将 ...