(java)自用StringUtils

package tuqu.no2.ddsd.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

 

public class StringUtils {
    /**
     * 检查是否为空
     * @param para
     * @return
     */
    public static boolean isNull(String para) {
        if (para == null || para.equals("null") || para.equals("")) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Sh1加密
     *
     * @param decript
     * @return
     */
    public static String SHA1(String decript) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(decript.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     *
     * [生成md5加密的字符串]
* [功能详细描述] * * @param originalString * @return */ public static String getMD5String(String originalString, int digits) { /*if (StringUtils.isNull(originalString)) { return ""; }*/ String md5 = null; char[] hexDigits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; byte[] bytes = originalString.getBytes(); try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes); byte[] md5Bytes = md.digest(); char[] str = new char[digits]; int k = 0; for (int i = 0; i < digits / 2; i++) { byte byte0 = md5Bytes[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } md5 = new String(str); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return md5; } public static String getMD5String(String originalString ) { return getMD5String(originalString,32); } /** * 是否是电话号码 */ public static boolean isMobileNum(String mobiles) { Pattern p = Pattern .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$"); Matcher m = p.matcher(mobiles.trim()); return m.matches(); } /** *是否是电话与手机号码 * * @param mobiles * @return
*/ public static boolean isMobileAndPhone(String mobiles) { String mobileRegexp = "((\\d{11})|^((\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})|(\\d{4}|\\d{3})-(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1})|(\\d{7,8})-(\\d{4}|\\d{3}|\\d{2}|\\d{1}))$)"; Pattern p = Pattern.compile(mobileRegexp); Matcher m = p.matcher(mobiles.trim()); return m.matches(); } }

你可能感兴趣的:((java)自用StringUtils)