版权声明
1. 本站文章和资源均来自互联网收集和整理,本站不承担任何责任及版权问题。
2. 相关版权归作者及其公司所有,仅供学习研究用途,请勿用于商业目的。
3. 若侵犯您的版权,请发邮件至webmaster@ishare1.cn联系我们,我们确认后将立即删除。

java判断字符串中是否包含中文?
方法1、针对每个字符判断
public static boolean isChinese(String str) throws UnsupportedEncodingException
{
int len = str.length();
for(int i = 0;i 0)
return true;
}
}
return false;
}
优缺点:
缺点:效率低【每次都需要循环检测字符串中每个字符】(每次发送都需要检测短信内容,每条内容有很多字符);
优点:不仅能检测出中文汉字还能检测中中文标点;
方法2、利用正则表达式
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、改造正则
/**
* 字符串是否包含中文
*
* @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视频教程
爱分享




