我是靠谱客的博主 飞快猎豹,这篇文章主要介绍java判断字符串中是否包含中文?,现在分享给大家,希望可以做个参考。

java判断字符串中是否包含中文?

方法1、针对每个字符判断

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public static boolean isChinese(String str) throws UnsupportedEncodingException { int len = str.length(); for(int i = 0;i < len;i ++) { String temp = URLEncoder.encode(str.charAt(i) + "", "utf-8"); if(temp.equals(str.charAt(i) + "")) continue; String[] codes = temp.split("%"); //判断是中文还是字符(下面判断不精确,部分字符没有包括) for(String code:codes) { if(code.compareTo("40") > 0) return true; } } return false; }
登录后复制

方法2、利用正则表达式

复制代码
1
2
3
4
5
6
7
8
9
public static boolean isContainChinese(String str) { Pattern p = Pattern.compile("[u4e00-u9fa5]"); Matcher m = p.matcher(str); if (m.find()) { return true; } return false; }
登录后复制

方法3、改造正则

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/** * 字符串是否包含中文 * * @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; }
登录后复制

推荐学习:Java视频教程

以上就是java判断字符串中是否包含中文?的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是飞快猎豹最近收集整理的关于java判断字符串中是否包含中文?的全部内容,更多相关java判断字符串中是否包含中文内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(98)

评论列表共有 0 条评论

立即
投稿
返回
顶部