Java 判断字符为中文实例代码(超管用)

 更新时间:2016年02月03日 11:33:30   投稿:mrr  
在做项目中经常会遇到有项目需求是需要判断字符为中文的一些问题,所以搜集了判断中文字符的代码片段,特此分享供大家参考

在做项目中经常会遇到有项目需求是需要判断字符为中文的一些问题,所以搜集了判断中文字符的代码片段,特此分享供大家参考。

直接贴出代码了,里面有详细的注释。

package com.coder4j.main;
import java.util.regex.Pattern;
/**
* Java 判断中文字符
* 
* @author Chinaxiang
* @date 2015-08-11
*
*/
public class CheckChinese {
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;
}
}

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

以上代码内容给大家介绍了Java 判断字符为中文实例代码(超管用),希望对大家有所帮助。

相关文章

  • Java并发编程之LockSupport类详解

    Java并发编程之LockSupport类详解

    LockSupport是一种线程阻塞工具,它可以在线程内任意位置让线程阻塞.接下来就带着大家详细了解一下LockSupport类,,需要的朋友可以参考下
    2021-05-05
  • WebClient抛UnsupportedMediaTypeException异常解决

    WebClient抛UnsupportedMediaTypeException异常解决

    这篇文章主要为大家介绍了WebClient抛UnsupportedMediaTypeException异常的解决方案,文中给大家介绍了六中方案,有需要的朋友可以借鉴参考下,希望能够有所帮助
    2022-02-02
  • Java如何将若干时间区间进行合并的方法步骤

    Java如何将若干时间区间进行合并的方法步骤

    这篇文章主要介绍了Java如何将若干时间区间进行合并的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Springboot结合Flowable实现工作流开发

    Springboot结合Flowable实现工作流开发

    本文主要介绍了Springboot结合Flowable实现工作流开发,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • MyBatis根据条件批量修改字段的方式

    MyBatis根据条件批量修改字段的方式

    这篇文章主要介绍了MyBatis根据条件批量修改字段的方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • Struts2实现上传单个文件功能

    Struts2实现上传单个文件功能

    这篇文章主要为大家详细介绍了Struts2实现上传单个文件功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • Spring的CorsFilter会失效的原因及解决方法

    Spring的CorsFilter会失效的原因及解决方法

    众所周知CorsFilter是Spring提供的跨域过滤器,我们可能会做以下的配置,基本上就是允许任何跨域请求,我利用Spring的CorsFilter做跨域操作但是出现报错,接下来小编就给大家介绍一Spring的CorsFilter会失效的原因及解决方法,需要的朋友可以参考下
    2023-09-09
  • springboot如何获取application.yml里值的方法

    springboot如何获取application.yml里值的方法

    这篇文章主要介绍了springboot如何获取application.yml里的值,文章围绕主题相关自资料展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-04-04
  • IDEA个性化设置注释模板详细讲解版

    IDEA个性化设置注释模板详细讲解版

    IDEA自带的注释模板不是太好用,我本人到网上搜集了很多资料系统的整理了一下制作了一份比较完整的模板来分享给大家,下面这篇文章主要给大家介绍了IDEA个性化设置注释模板的相关资料,需要的朋友可以参考下
    2024-01-01
  • Spring Bean装载方式代码实例解析

    Spring Bean装载方式代码实例解析

    这篇文章主要介绍了Spring Bean装载方式代码实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02

最新评论