java元帅

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
/*判断字符串中是否仅包含字母数字和汉字
     *各种字符的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;
    }

 

posted on 2022-09-21 13:21  java元帅  阅读(151)  评论(0)    收藏  举报