java判断字符串是否包含汉字工具类

 

 

 

/**
* 判断字符串中是否包含中文
*
* @param str 待校验字符串
* @return 是否为中文
* @warn 不能校验是否为中文标点符号
*/
public static boolean isContainsChinese(String str) {
if (str == null) { return false; }
Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
Matcher m = p.matcher(str);
return m.find();
}

 

/**
* 判断字符串中是否全是中文
*
* @param str 待校验字符串
* @return 是否全是中文
*/
public static boolean isAllChinese(String str) {
if (str == null) { return false; }
Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
Matcher m = p.matcher(str);
return m.matches();
}

 

posted @ 2023-04-12 11:56  kelelipeng  阅读(165)  评论(0编辑  收藏  举报