在做項目中經常會遇到有項目需求是需要判斷字符為中文的一些問題,所以蒐集了判斷中文字符的代碼片段,特此分享供大家參考。
直接貼出代碼了,裡面有詳細的註釋。
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));// falseSystem.out.println("s2是否包含中文:" + hasChineseByRange(s2));// trueSystem.out.println("s3是否包含中文:" + hasChineseByRange(s3));// trueSystem.out.println("s4是否包含中文:" + hasChineseByRange(s4));// falseSystem.out.println("s5是否包含中文:" + hasChineseByRange(s5));// falseSystem.out.println("s6是否包含中文:" + hasChineseByRange(s6));// falseSystem.out.println("s7是否包含中文:" + hasChineseByRange(s7));// falseSystem.out.println("s8是否包含中文:" + hasChineseByRange(s8));// falseSystem.out.println("s9是否包含中文:" + hasChineseByRange(s9));// trueSystem.out.println("-------分割線-------");System.out.println("s1是否全是中文:" + isChineseByRange(s1));// falseSystem.out.println("s2是否全是中文:" + isChineseByRange(s2));// trueSystem.out.println("s3是否全是中文:" + isChineseByRange(s3));// false 中文標點不在範圍內System.out.println("s4是否全是中文:" + isChineseByRange(s4));// falseSystem.out.println("s5是否全是中文:" + isChineseByRange(s5));// falseSystem.out.println("s6是否全是中文:" + isChineseByRange(s6));// falseSystem.out.println("s7是否全是中文:" + isChineseByRange(s7));// falseSystem.out.println("s8是否全是中文:" + isChineseByRange(s8));// falseSystem.out.println("s9是否全是中文:" + isChineseByRange(s9));// trueSystem.out.println("-------分割線-------");// 2 使用字符範圍正則判斷(結果同1)System.out.println("s1是否包含中文:" + hasChineseByReg(s1));// falseSystem.out.println("s2是否包含中文:" + hasChineseByReg(s2));// trueSystem.out.println("s3是否包含中文:" + hasChineseByReg(s3));// trueSystem.out.println("s4是否包含中文:" + hasChineseByReg(s4));// falseSystem.out.println("s5是否包含中文:" + hasChineseByReg(s5));// falseSystem.out.println("s6是否包含中文:" + hasChineseByReg(s6));// falseSystem.out.println("s7是否包含中文:" + hasChineseByReg(s7));// falseSystem.out.println("s8是否包含中文:" + hasChineseByReg(s8));// falseSystem.out.println("s9是否包含中文:" + hasChineseByReg(s9));// trueSystem.out.println("-------分割線-------");System.out.println("s1是否全是中文:" + isChineseByReg(s1));// falseSystem.out.println("s2是否全是中文:" + isChineseByReg(s2));// trueSystem.out.println("s3是否全是中文:" + isChineseByReg(s3));// false 中文標點不在範圍內System.out.println("s4是否全是中文:" + isChineseByReg(s4));// falseSystem.out.println("s5是否全是中文:" + isChineseByReg(s5));// falseSystem.out.println("s6是否全是中文:" + isChineseByReg(s6));// falseSystem.out.println("s7是否全是中文:" + isChineseByReg(s7));// falseSystem.out.println("s8是否全是中文:" + isChineseByReg(s8));// falseSystem.out.println("s9是否全是中文:" + isChineseByReg(s9));// trueSystem.out.println("-------分割線-------");// 3 使用CJK字符集判斷System.out.println("s1是否包含中文:" + hasChinese(s1));// falseSystem.out.println("s2是否包含中文:" + hasChinese(s2));// trueSystem.out.println("s3是否包含中文:" + hasChinese(s3));// trueSystem.out.println("s4是否包含中文:" + hasChinese(s4));// falseSystem.out.println("s5是否包含中文:" + hasChinese(s5));// falseSystem.out.println("s6是否包含中文:" + hasChinese(s6));// falseSystem.out.println("s7是否包含中文:" + hasChinese(s7));// falseSystem.out.println("s8是否包含中文:" + hasChinese(s8));// falseSystem.out.println("s9是否包含中文:" + hasChinese(s9));// trueSystem.out.println("-------分割線-------");System.out.println("s1是否全是中文:" + isChinese(s1));// falseSystem.out.println("s2是否全是中文:" + isChinese(s2));// trueSystem.out.println("s3是否全是中文:" + isChinese(s3));// true 中文標點也被包含進來System.out.println("s4是否全是中文:" + isChinese(s4));// falseSystem.out.println("s5是否全是中文:" + isChinese(s5));// falseSystem.out.println("s6是否全是中文:" + isChinese(s6));// falseSystem.out.println("s7是否全是中文:" + isChinese(s7));// falseSystem.out.println("s8是否全是中文:" + isChinese(s8));// falseSystem.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 判斷字符為中文實例代碼(超管用),希望對大家有所幫助。