/*判断字符串中是否仅包含字母数字和汉字 *各种字符的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; }
分类:
note
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了