欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java 判斷字符為中文實(shí)例代碼(超管用)

 更新時(shí)間:2016年02月03日 11:33:30   投稿:mrr  
在做項(xiàng)目中經(jīng)常會(huì)遇到有項(xiàng)目需求是需要判斷字符為中文的一些問(wèn)題,所以搜集了判斷中文字符的代碼片段,特此分享供大家參考

在做項(xiàng)目中經(jīng)常會(huì)遇到有項(xiàng)目需求是需要判斷字符為中文的一些問(wèn)題,所以搜集了判斷中文字符的代碼片段,特此分享供大家參考。

直接貼出代碼了,里面有詳細(xì)的注釋。

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.!@#$%^&*()_+-={}|[];':\"?";
// 純中文(不含中文標(biāo)點(diǎn))
String s2 = "你好中國(guó)";
// 純中文(含中文標(biāo)點(diǎn))
String s3 = "你好,中國(guó)?!丁罚骸啊薄?;()【】?。ぁ?;
// 韓文
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 中文標(biāo)點(diǎn)不在范圍內(nèi)
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 使用字符范圍正則判斷(結(jié)果同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 中文標(biāo)點(diǎn)不在范圍內(nèi)
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 中文標(biāo)點(diǎn)也被包含進(jìn)來(lái)
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>
* 包含中文標(biāo)點(diǎn)符號(hào)<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>
* 包含中文標(biāo)點(diǎn)符號(hào)<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>
* 包含中文標(biāo)點(diǎn)符號(hào)<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>
* 根據(jù)漢字編碼范圍進(jìn)行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,?!丁罚ǎ啊?”、?。さ确?hào))<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>
* 根據(jù)漢字編碼范圍進(jìn)行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,?!丁罚ǎ啊?”、!¥等符號(hào))<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>
* 根據(jù)漢字編碼范圍進(jìn)行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,。《》()“‘'”、?。さ确?hào))<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>
* 根據(jù)漢字編碼范圍進(jìn)行判斷<br>
* CJK統(tǒng)一漢字(不包含中文的,。《》()“‘'”、?。さ确?hào))<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;
}
}

如果僅僅去判斷是否是中文,不需判斷中文標(biāo)點(diǎn)的話,推薦使用正則去匹配,可能更高效點(diǎn)。

以上代碼內(nèi)容給大家介紹了Java 判斷字符為中文實(shí)例代碼(超管用),希望對(duì)大家有所幫助。

相關(guān)文章

  • Java并發(fā)編程之LockSupport類(lèi)詳解

    Java并發(fā)編程之LockSupport類(lèi)詳解

    LockSupport是一種線程阻塞工具,它可以在線程內(nèi)任意位置讓線程阻塞.接下來(lái)就帶著大家詳細(xì)了解一下LockSupport類(lèi),,需要的朋友可以參考下
    2021-05-05
  • WebClient拋UnsupportedMediaTypeException異常解決

    WebClient拋UnsupportedMediaTypeException異常解決

    這篇文章主要為大家介紹了WebClient拋UnsupportedMediaTypeException異常的解決方案,文中給大家介紹了六中方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2022-02-02
  • Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟

    Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟

    這篇文章主要介紹了Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Springboot結(jié)合Flowable實(shí)現(xiàn)工作流開(kāi)發(fā)

    Springboot結(jié)合Flowable實(shí)現(xiàn)工作流開(kāi)發(fā)

    本文主要介紹了Springboot結(jié)合Flowable實(shí)現(xiàn)工作流開(kāi)發(fā),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • MyBatis根據(jù)條件批量修改字段的方式

    MyBatis根據(jù)條件批量修改字段的方式

    這篇文章主要介紹了MyBatis根據(jù)條件批量修改字段的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Struts2實(shí)現(xiàn)上傳單個(gè)文件功能

    Struts2實(shí)現(xiàn)上傳單個(gè)文件功能

    這篇文章主要為大家詳細(xì)介紹了Struts2實(shí)現(xiàn)上傳單個(gè)文件功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • Spring的CorsFilter會(huì)失效的原因及解決方法

    Spring的CorsFilter會(huì)失效的原因及解決方法

    眾所周知CorsFilter是Spring提供的跨域過(guò)濾器,我們可能會(huì)做以下的配置,基本上就是允許任何跨域請(qǐng)求,我利用Spring的CorsFilter做跨域操作但是出現(xiàn)報(bào)錯(cuò),接下來(lái)小編就給大家介紹一Spring的CorsFilter會(huì)失效的原因及解決方法,需要的朋友可以參考下
    2023-09-09
  • springboot如何獲取application.yml里值的方法

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

    這篇文章主要介紹了springboot如何獲取application.yml里的值,文章圍繞主題相關(guān)自資料展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-04-04
  • IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版

    IDEA個(gè)性化設(shè)置注釋模板詳細(xì)講解版

    IDEA自帶的注釋模板不是太好用,我本人到網(wǎng)上搜集了很多資料系統(tǒng)的整理了一下制作了一份比較完整的模板來(lái)分享給大家,下面這篇文章主要給大家介紹了IDEA個(gè)性化設(shè)置注釋模板的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • Spring Bean裝載方式代碼實(shí)例解析

    Spring Bean裝載方式代碼實(shí)例解析

    這篇文章主要介紹了Spring Bean裝載方式代碼實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02

最新評(píng)論