StringUtil对字符串类型参数进行校验的工具类

本文介绍了一个实用的字符串校验工具类,包括对空值、空字符串的判断及格式化处理等功能。提供了多种字符串操作方法,如手机号验证、XSS清理等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.对字符串类型参数进行校验的工具类

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    public static boolean isNull(Object... obj){
        return !isNotNull(obj);
    }

    public static boolean isNotNull(Object... obj){
        if(obj != null){
            for (Object o : obj) {
                if(o == null){
                    return false;
                }
            }
        }else{
            return false;
        }
        return true;
    }

    public static boolean isEmpty(String key) {
        return  !isNotEmpty(key);
    }

    public static boolean isNotEmpty(String key) {
        if (key!=null && !"".equals(key.trim())){
            return true;
        }
        return false;
    }

    public static boolean isNotEmpty(String... keys) {
        return  !isEmpty(keys);
    }

    public static boolean isEmpty(String... keys) {
        for (String key : keys) {
            if (StringUtil.isEmpty(key)) {
                return true;
            }
        }
        return false;
    }

    public static String decimalFormatPrice(String param) {
        if (param == null || "".equals(param)) {
            param = "0.00";
        }
        double tmp = Double.parseDouble(param);
        DecimalFormat df = new DecimalFormat("0.00");
        return df.format(tmp);
    }

    public static String cleanXss(String str) {
        if (str == null || "".equals(str.trim())) {
            return "";
        }
        str = str.replaceAll(" ", "");
        return str;
    }

    /**
    * @param str
    * @param trim
    * @return
    * @description 以*(分号)分隔的字符串,去除首尾的*(分号)。
    * @author qinhc
    * @createTime 2015上午11:05:00
    */
    public static String StrRemoveTrim(String str, String trim) {
        // str=;2;;
        String resultStr = "";
        String[] strList = str.split(trim);
        for (String s : strList) {
            if (!"".equals(s) && null != s) {
                resultStr = resultStr + s + trim;
            }
        }
        if (resultStr.length() > 0) {
            resultStr = resultStr.substring(0, resultStr.length() - 1);
        }
        return resultStr;
    }

    /**
    * 是否手机号 简单校验11位数字
    *
    * @param str
    * @return
    * @author wangrq1
    */
    public static boolean isPhone(String str) {
        Matcher m = NUM_11.matcher(str);
        return m.matches();
    }

    static Pattern NUM_11 = Pattern.compile("\\d{11}");

    /**
    * 生产指定长度的由大写字母和数字组成的字符串 add by yezhenyue on 2016-03-29
    * @param len
    * @return
    */
    public static String randomStr(int len) {
        if (len == 0) {
            return "";
        }
        int a = (int) (Math.random() * 2);
        if (a == 0) {
            return ((int) (Math.random() * 10)) + randomStr(len - 1);
        } else {
            return ((char) ((int) (Math.random() * 26) + 65)) + randomStr(len - 1);
        }
    }

    public static void main(String[] args) {

        System.out.println(isPhone("18001141691"));
        System.out.println(isPhone(""));

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值