判断字符串是否数字、英文字母、汉字

威哥今天好忙,不废话了,直接代码吧,大家一起参考参考。

    /**
     * 判断字符串是否为纯数字
     * Author:William(徐威)
     * Create Time:2018-12-05
     *
     * @param str
     * @return
     */
    public static Boolean isNumber(String str) {
        String pattern = "^[0-9]+$";
     
        return str.matches(pattern);
    }
     
    /**
     * 判断字符串是否为纯字母
     * Author:William(徐威)
     * Create Time:2018-12-05
     *
     * @param str
     * @return
     */
    public static Boolean isEnglish(String str) {
        String pattern = "^[A-Za-z]+$";
     
        return str.matches(pattern);
    }
     
    /**
     * 判断字符串是否为纯汉字
     * Author:William(徐威)
     * Create Time:2018-12-05
     *
     * @param str
     * @return
     */
    public static Boolean isChinese(String str) {
        String pattern = "^[\u4e00-\u9fa5]+$";
     
        return str.matches(pattern);
    }

如果是判断字符串中是否含有数字、字母、汉字等等,可以参照威哥下面的方法:

/**
 * 判断字符串是否含有汉字
 * Author:William(徐威)
 * Create Time:2018-12-05
 *
 * @param str
 * @return
 */
public static Boolean isIncludeChinese(String str) {
    Boolean flag = false;
    for (int i = 0; i < str.length(); i++) {
        String ss = String.valueOf(str.charAt(i));
        if (isChinese(ss)) {
            flag = true;
            break;
        }
    }

    return flag;
}


---------------------
作者:平凡的威哥
来源:CSDN
原文:https://blog.csdn.net/xuwei_net/article/details/84841105
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/xuwei_net/article/details/84841105