检测字符串是否包含中文

    import org.apache.commons.lang3.StringUtils;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    /**
     * 字符串是否包含中文
     *
     * @param str 待校验字符串
     * @return true 包含中文字符||字符串为空  false 不包含中文字符
     */
    public static boolean isContainChinese(String str) {
        if (StringUtils.isEmpty(str)) {
            return true;
        }
        Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
        Matcher m = p.matcher(str);
        if (m.find()) {
            return true;
        }
        return false;
    }


测试:

System.out.println(InfoUtil.isContainChinese(""));//true
System.out.println(InfoUtil.isContainChinese("abc我"));//true
System.out.println(InfoUtil.isContainChinese("abc123"));//false
System.out.println(InfoUtil.isContainChinese("abc"));//false

猜你喜欢

转载自blog.csdn.net/xianhenyuan/article/details/82259400
今日推荐