工具類(判斷字符串空和null)

package com.mybatis.api.mybatis.util;


import java.lang.reflect.Array;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 *
 * 字符串工具類
 */
public class StringUtil {

    public static  Boolean isEmpty(Object type){
        if(type != null && !"".equals(type)){
            return false;
        }
        return true;
    }

    public static Boolean isListEmpty(List list){
        if(list != null && list.size() > 0){
            return false;
        }
        return true;
    }

    /**
     * 判斷對象爲空字符串或者爲null,如果滿足其中一個條件,則返回true
     *
     * @param
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static boolean isNullOrEmpty(Object obj) {
        boolean isEmpty = false;
        if (obj == null) {
            isEmpty = true;
        } else if (obj instanceof String) {
            isEmpty = ((String) obj).trim().isEmpty();
        } else if (obj instanceof Collection) {
            isEmpty = (((Collection) obj).size() == 0);
        } else if (obj instanceof Map) {
            isEmpty = ((Map) obj).size() == 0;
        } else if (obj.getClass().isArray()) {
            isEmpty = Array.getLength(obj) == 0;
        }
        return isEmpty;
    }

    /**
     * 檢查 email輸入是否正確 正確的書寫格 式爲 username@domain
     *
     * @param text
     * @return
     */
    public static boolean checkEmail(String text, int length) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*") && text.length() <= length;
    }

    /**
     * 檢查電話輸入 是否正確 正確格 式 012-87654321、0123-87654321、0123-7654321
     *
     * @param text
     * @return
     */
    public static boolean checkTelephone(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches(
                "(0\\d{2,3}-\\d{7,8})|" +
                        "(0\\d{9,11})|" +
                        "(\\d{7})|" +
                        "(\\d{8})|" +
                        "(4\\d{2}\\d{7})|" +
                        "(4\\d{2}-\\d{7})|" +
                        "(4\\d{2}-\\d{3}-\\d{4})|" +
                        "(4\\d{2}-\\d{4}-\\d{3})");
    }

    /**
     * 檢查手機輸入 是否正確
     *
     * @param text
     * @return
     */
    public static boolean checkMobilephone(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("^1(3[0-9]|4[579]|5[0-35-9]|8[0-9]|7[015-8])\\d{8}$");
    }

    /**
     * 檢查中文名輸 入是否正確
     *
     * @param text
     * @return
     */
    public static boolean checkChineseName(String text, int length) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("^[\u4e00-\u9fa5]+$") && text.length() <= length;
    }

    /**
     * 檢查字符串中是否有空格,包括中間空格或者首尾空格
     *
     * @param text
     * @return
     */
    public static boolean checkBlank(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("^\\s*|\\s*$");
    }

    /**
     * 檢查字符串是 否含有HTML標籤
     *
     * @param text
     * @return
     */

    public static boolean checkHtmlTag(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("<(\\S*?)[^>]*>.*?<!--\\1-->|<.*? />");
    }

    /**
     * 檢查URL是 否合法
     *
     * @param text
     * @return
     */
    public static boolean checkURL(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("[a-zA-z]+://[^\\s]*");
    }

    /**
     * 檢查IP是否 合法
     *
     * @param text
     * @return
     */
    public static boolean checkIP(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}+\\.\\d{1,3}");
    }



    /**
     * 檢查QQ是否 合法,必須是數字,且首位不能爲0,最長15位
     *
     * @param text
     * @return
     */

    public static boolean checkQQ(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("[1-9][0-9]{4,13}");
    }

    /**
     * 檢查郵編是否 合法
     *
     * @param text
     * @return
     */
    public static boolean checkPostCode(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("[1-9]\\d{5}(?!\\d)");
    }

    /**
     * 檢查身份證是 否合法,15位或18位(或者最後一位爲X)
     *
     * @param text
     * @return
     */
    public static boolean checkIDCard(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("\\d{15}|\\d{18}|(\\d{17}[x|X])");
    }

    /**
     * 檢查輸入是否 超出規定長度
     *
     * @param length
     * @param text
     * @return
     */
    public static boolean checkLength(String text, int length) {
        return ((StringUtil.isNullOrEmpty(text)) ? 0 : text.length()) <= length;
    }

    /**
     * 判斷是否爲數字
     * @param text
     * @return
     */
    public static boolean isNumber(String text) {
        if(StringUtil.isNullOrEmpty(text)){
            return false;
        }
        return text.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章