java中文字符_Java判斷中文字符

packagecom.jsoft.test;importjava.util.regex.Pattern;/*** 判斷中文字符

*

*@authorjim

* @date 2017-12-22*/

public classChineseHelper {public static voidmain(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

}/*** 是否包含中文字符

* 包含中文標點符號

*

*@paramstr

*@return

*/

public static booleanhasChinese(String str) {if (str == null) {return false;

}char[] ch =str.toCharArray();for (charc : ch) {if(isChinese(c)) {return true;

}

}return false;

}/*** 是否全是中文字符

* 包含中文標點符號

*

*@paramstr

*@return

*/

public static booleanisChinese(String str) {if (str == null) {return false;

}char[] ch =str.toCharArray();for (charc : ch) {if (!isChinese(c)) {return false;

}

}return true;

}/*** 是否是中文字符

* 包含中文標點符號

*

*@paramc

*@return

*/

private static boolean isChinese(charc) {

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;

}/*** 是否包含漢字

* 根據漢字編碼范圍進行判斷

* CJK統一漢字(不包含中文的,。《》()“‘'”、!¥等符號)

*

*@paramstr

*@return

*/

public static booleanhasChineseByReg(String str) {if (str == null) {return false;

}

Pattern pattern= Pattern.compile("[\\u4E00-\\u9FBF]+");returnpattern.matcher(str).find();

}/*** 是否全是漢字

* 根據漢字編碼范圍進行判斷

* CJK統一漢字(不包含中文的,。《》()“‘'”、!¥等符號)

*

*@paramstr

*@return

*/

public static booleanisChineseByReg(String str) {if (str == null) {return false;

}

Pattern pattern= Pattern.compile("[\\u4E00-\\u9FBF]+");returnpattern.matcher(str).matches();

}/*** 是否包含漢字

* 根據漢字編碼范圍進行判斷

* CJK統一漢字(不包含中文的,。《》()“‘'”、!¥等符號)

*

*@paramstr

*@return

*/

public static booleanhasChineseByRange(String str) {if (str == null) {return false;

}char[] ch =str.toCharArray();for (charc : ch) {if (c >= 0x4E00 && c <= 0x9FBF) {return true;

}

}return false;

}/*** 是否全是漢字

* 根據漢字編碼范圍進行判斷

* CJK統一漢字(不包含中文的,。《》()“‘'”、!¥等符號)

*

*@paramstr

*@return

*/

public static booleanisChineseByRange(String str) {if (str == null) {return false;

}char[] ch =str.toCharArray();for (charc : ch) {if (c < 0x4E00 || c > 0x9FBF) {return false;

}

}return true;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值