个人常用工具类 --- StringUtli

工具类 – 个人常用工具类String

public class StringUtile{

    /**
     * uuid编码
     */
    public static final String REGEX_UUID = "[0-9a-zA-Z]{32}";

    /**
     * 假设编码对应城市,省分编码
     */
    private static final String cityCode[] = {"11", "12", "13", "14", "15", "21", "22", "23", "31", "32", "33", "34",
            "35", "36", "37", "41", "42", "43", "44", "45", "46", "50", "51", "52", "53", "54", "61", "62", "63", "64",
            "65", "71", "81", "82", "91"};

    /**
     * 每位加权因子
     */
    private static int power[] = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };



    /**
     * 手机号码
     */
    private static final String phoneRegular = "^1[0-9]{10}$";

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }

    /**
     * 按字符数截短<BR>
     * 发生截短时将在字符串尾添加...(在size<3时不添加)<BR>
     * 返回的字符串的字符数<=size
     */
    public static String shortenByChars(String string, int size) {
        if (size < 0) {
            throw new IllegalArgumentException("size can not be negative");
        }
        if (string == null || string.length() <= size) {
            return string;
        }
        if (size < 3) {
            return string.substring(0, size);
        }
        return string.substring(0, size - 3) + "...";
    }

    /**
     * 判定字符串是否为空
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }


    /**
     * 使用java正则表达式去掉多余的.与0
     */
    public static String subZeroAndDot(String s) {
        if (s.indexOf(".") > -1) {
            s = s.replaceAll("0+?$", "");// 去掉多余的0
            s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
        }
        return s;
    }

    /**
     * 手机号码 隐藏位 123****5555
     */
    public static String getHiddenPhone(String phone) {
        String hiddenPhone = phone;
        if (isEmpty(phone)) {
            hiddenPhone = "";
        }
        if (phone.length() == 11) {
            hiddenPhone = phone.substring(0, 3) + "****" + phone.substring(7);
        }
        return hiddenPhone;
    }

    /**
     * 获取32位的UUID
     */
    public static String getUuid32() {
        return UUID.randomUUID().toString().replace("-", "");
    }


    /**
     * 判断是否手机号码 (11位数字)功能描述: <br>
     */
    public static boolean checkIsPhone(String phone) {
        if (isEmpty(phone)) {
            return false;
        }
        return phone.matches(phoneRegular);
    }


    public static boolean isNotNull(String s) {
        if ((null != s) && (s.trim().length() != 0))
            return true;
        return false;
    }

    public static boolean isNull(String s) {
        if ((null == s) || (s.trim().length() == 0))
            return true;
        return false;
    }

    /**
     * 校验省份
     */
    private static boolean checkProvinceid(String provinceid) {
        for (String id : cityCode) {
            if (id.equals(provinceid)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 邮箱号码隐位
     */
    public static String getHiddenEmail(String email) {
        String hiddenEmail = email;
        if (isEmpty(email)) {
            hiddenEmail = "";
        }
        if (email.contains("@")) {
            String[] emails = email.split("@");
            String name = emails[0];
            // xx****x@xx.com 取前2后1规则
            if (name.length() > 1) {
                name = name.substring(0, 2) + "***" + name.substring(name.length() - 1);
            } else {
                // x@XX.com,则显示为xx****x@XX.com 取前2后1规则
                name = name + name + "***" + name;
            }
            hiddenEmail = name + "@" + emails[1];
        }
        return hiddenEmail;
    }

    /**
     * 判断是否为uuid
     */
    public static boolean isUuid(String str) {
        if (isBlank(str)) {
            return false;
        }

        if (length(str) != 32) {
            return false;
        }

        return Pattern.matches(REGEX_UUID, str);
    }


    /**
     * 字符串前面补0
     */
    public static String addZeroLeft(String str, int strLength) {
        int strLen = str.length();
        if (strLen < strLength) {
            while (strLen < strLength) {
                StringBuffer sb = new StringBuffer();
                sb.append("0").append(str);// 左补0
                str = sb.toString();
                strLen = str.length();
            }
        }

        return str;
    }


    /**
     * 是否是数值
     */
    public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 数字验证
     */
    private static boolean isDigital(String str) {
        return str.matches("^[0-9]*$");
    }

    /**
     * 18位身份证号校验
     */
    public static boolean validate18Idcard(String idcard) {
        if (idcard == null) {
            return false;
        }
        // 非18位为假
        if (idcard.length() != 18) {
            return false;
        }
        // 获取前17位
        String idcard17 = idcard.substring(0, 17);

        // 前17位全部为数字
        if (!isDigital(idcard17)) {
            return false;
        }

        String provinceid = idcard.substring(0, 2);
        // 校验省份
        if (!checkProvinceid(provinceid)) {
            return false;
        }

        // 校验出生日期
        String birthday = idcard.substring(6, 14);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

        try {
            Date birthDate = sdf.parse(birthday);
            String tmpDate = sdf.format(birthDate);
            if (!tmpDate.equals(birthday)) {// 出生年月日不正确
                return false;
            }

        } catch (ParseException e1) {

            return false;
        }
        // 获取第18位
        String idcard18Code = idcard.substring(17, 18);

        char c[] = idcard17.toCharArray();

        int bit[] = converCharToInt(c);

        int sum17 = 0;

        sum17 = getPowerSum(bit);

        // 将和值与11取模得到余数进行校验码判断
        String checkCode = getCheckCodeBySum(sum17);
        if (null == checkCode) {
            return false;
        }
        // 将身份证的第18位与算出来的校码进行匹配,不相等就为假
        if (!idcard18Code.equalsIgnoreCase(checkCode)) {
            return false;
        }

        return true;

    }

    /**
     * 将字符数组转为整型数组
     */
    private static int[] converCharToInt(char[] c) throws NumberFormatException {
        int[] a = new int[c.length];
        int k = 0;
        for (char temp : c) {
            a[k++] = Integer.parseInt(String.valueOf(temp));
        }
        return a;
    }

    /**
     * 将身份证的每位和对应位的加权因子相乘之后,再得到和值
     */
    private static int getPowerSum(int[] bit) {

        int sum = 0;

        if (power.length != bit.length) {
            return sum;
        }

        for (int i = 0; i < bit.length; i++) {
            for (int j = 0; j < power.length; j++) {
                if (i == j) {
                    sum = sum + bit[i] * power[j];
                }
            }
        }
        return sum;
    }

    /**
     * 将和值与11取模得到余数进行校验码判断
     */
    private static String getCheckCodeBySum(int sum17) {
        String checkCode = null;
        switch (sum17 % 11) {
            case 10:
                checkCode = "2";
                break;
            case 9:
                checkCode = "3";
                break;
            case 8:
                checkCode = "4";
                break;
            case 7:
                checkCode = "5";
                break;
            case 6:
                checkCode = "6";
                break;
            case 5:
                checkCode = "7";
                break;
            case 4:
                checkCode = "8";
                break;
            case 3:
                checkCode = "9";
                break;
            case 2:
                checkCode = "x";
                break;
            case 1:
                checkCode = "0";
                break;
            case 0:
                checkCode = "1";
                break;
        }
        return checkCode;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值