java元帅

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  49 随笔 :: 0 文章 :: 0 评论 :: 19902 阅读
复制代码
/*判断字符串中是否仅包含字母数字和汉字
     *各种字符的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   java元帅  阅读(142)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示