package com.inesa.water.affairs.water.util;
import static java.lang.Character.UnicodeBlock.*;
import com.google.common.base.Strings;
public class CheckContainChinese {
public static void main(String[] args) {
String containChinese = "test,我有中文";
String containNoChiese = "test, i don't contain chinese";
System.out.println("containChinese 是否包含中文 :" + checkStringContainChinese(containChinese));
System.out.println("containNoChiese 是否包含中文 :" + checkStringContainChinese(containNoChiese));
}
public static boolean checkStringContainChinese(String checkStr){
if(!Strings.isNullOrEmpty(checkStr)){
char[] checkChars = checkStr.toCharArray();
for(int i = 0; i < checkChars.length; i++){
char checkChar = checkChars[i];
if(checkCharContainChinese(checkChar)){
return true;
}
}
}
return false;
}
public static boolean checkCharContainChinese(char checkChar){
Character.UnicodeBlock ub = Character.UnicodeBlock.of(checkChar);
if(CJK_UNIFIED_IDEOGRAPHS == ub || CJK_COMPATIBILITY_IDEOGRAPHS == ub || CJK_COMPATIBILITY_FORMS == ub ||
CJK_RADICALS_SUPPLEMENT == ub || CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A == ub || CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B == ub){
return true;
}
return false;
}
}