/*判断字符串中是否仅包含字母数字和汉字 *各种字符的unicode编码的范围: * 汉字:[0x4e00,0x9fa5](或十进制[19968,40869]) * 数字:[0x30,0x39](或十进制[48, 57]) *小写字母:[0x61,0x7a](或十进制[97, 122]) * 大写字母:[0x41,0x5a](或十进制[65, 90]) */ public static boolean isLetterDigitOrChinese(String str) { String regex = "^[a-z0-9A-Z\u4e00-\u9fa5]+$"; return str.matches(regex); }
/** * 优点:利用正则效率高; * 缺点:只能检测出中文汉字不能检测中文标点; * @param str * @return */ public static boolean isContainChinese(String str) { Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); Matcher m = p.matcher(str); if (m.find()) { return true; } return false; }
/** 效率既高又能检测出中文汉字和中文标点; * 字符串是否包含中文 * * @param str 待校验字符串 * @return true 包含中文字符 false 不包含中文字符 * @throws EmptyException */ public static boolean isContainChinese(String str) throws EmptyException { if (StringUtils.isEmpty(str)) { throw new EmptyException("sms context is empty!"); } Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]"); Matcher m = p.matcher(str); if (m.find()) { return true; } return false; }
浙公网安备 33010602011771号