All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.loocme.sys.util.StringUtil Maven / Gradle / Ivy

There is a newer version: 7.1.11
Show newest version
package com.loocme.sys.util;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.loocme.sys.constance.DateFormatConst;
import com.loocme.sys.constance.RegConst;
import lombok.extern.slf4j.Slf4j;

/**
 * 字符串使用帮助类
 *
 * @author loocme
 */
@Slf4j
public class StringUtil
{

    private static final String CONST_STRING_NULL = "null";
    private static final String CONST_BOOLEAN_TRUE_ON = "on";
    private static final String CONST_BOOLEAN_TRUE_TRUE = "true";
    private static final String CONST_BOOLEAN_TRUE_1 = "1";
    private static final String CONST_BOOLEAN_FALSE_0 = "0";
    private static final String CONST_DIGIT_E_DOWN = "-";
    private static final int CONST_DIGIT_E_DOWN_INDEX = 5;
    private static final int CONST_DIGIT_E_VALUE_INDEX = 6;

    /**
     * 判断字符串是否为空
     *
     * @param str 进行判断的字符串
     * @return 是否为空
     */
    public static boolean isNull(String str)
    {
        if (null == str || PatternUtil.isMatch(str, RegConst.STRING_ISNULL))
        {
            return true;
        }
        if (CONST_STRING_NULL.equals(str))
        {
            return true;
        }
        return false;
    }

    /**
     * 判断字符串是否不为空
     *
     * @param str 进行判断的字符串
     * @return 是否非空
     */
    public static boolean isNotNull(String str)
    {
        return !isNull(str);
    }

    /**
     * 获取short值
     *
     * @param str 源字符串
     * @return 获取的int值
     */
    public static short getShort(String str)
    {
        Double d = getDouble(str);
        return d.shortValue();
    }

    /**
     * 获取int值
     *
     * @param str 源字符串
     * @return 获取的int值
     */
    public static int getInteger(String str)
    {
        Double d = getDouble(str);
        return d.intValue();
    }

    public static boolean isInteger(String str)
    {
        return PatternUtil.isMatch(str, RegConst.INT);
    }

    public static boolean isDouble(String str)
    {
        return PatternUtil.isMatch(str, RegConst.DOUBLE);
    }

    /**
     * 获取long值
     *
     * @param str 源字符串
     * @return 获取的long值
     */
    public static long getLong(String str)
    {
        Double d = getDouble(str);
        return d.longValue();
    }

    /**
     * 获取float值
     *
     * @param str 源字符串
     * @return 获取的float值
     */
    public static float getFloat(String str)
    {
        Double d = getDouble(str);
        return d.floatValue();
    }

    /**
     * 获取double值
     *
     * @param str 源字符串
     * @return 获取的double值
     */
    public static double getDouble(String str)
    {
        if (StringUtil.isNull(str))
        {
            return 0;
        }

        int zf = 0;
        int length = 0;
        double value = 0.0;
        String[] seArr = PatternUtil.getMatch(str, RegConst.DOUBLE, Pattern.CASE_INSENSITIVE);
        if (null == seArr)
        {
            log.error(String.format("数据转换异常:从类型(String)值(%s)转换为类型(Double)", str));
            return 0;
        }
        value = Double.parseDouble(seArr[1]);
        if (StringUtil.isNotNull(seArr[CONST_DIGIT_E_DOWN_INDEX]) && CONST_DIGIT_E_DOWN.equals(seArr[CONST_DIGIT_E_DOWN_INDEX]))
        {
            zf = 1;
        }
        if (StringUtil.isNotNull(seArr[CONST_DIGIT_E_VALUE_INDEX]))
        {
            length = StringUtil.getInteger(seArr[CONST_DIGIT_E_VALUE_INDEX]);
        }
        if (0 < length)
        {
            for (int i = 0; i < length; i++)
            {
                value = (1 == zf) ? (value / 10.0) : (value * 10.0);
            }
        }
        return value;
    }

    /**
     * 获取boolean值
     *
     * @param str 源字符串
     * @return 获取的double值
     */
    public static boolean getBoolean(String str)
    {
        if (StringUtil.isNull(str))
        {
            return false;
        }
        if (str.trim().equals(CONST_BOOLEAN_FALSE_0))
        {
            return false;
        }
        if (str.trim().equals(CONST_BOOLEAN_TRUE_1) || CONST_BOOLEAN_TRUE_TRUE.equalsIgnoreCase(str.trim()) || CONST_BOOLEAN_TRUE_ON.equalsIgnoreCase(str.trim()))
        {
            return true;
        }
        return false;
    }

    /**
     * 获取四舍五入后的数字,保留两位小数
     *
     * @param str 源数,字符串格式
     * @return 四舍五入后的数
     */
    public static String decimal(String str)
    {
        return decimal(str, 2);
    }

    /**
     * 获取四舍五入后的数字,保留两位小数
     *
     * @param value 源数,double
     * @return 四舍五入后的数
     */
    public static String decimal(Double value)
    {
        return decimal(value, 2);
    }

    /**
     * 获取四舍五入后的数字
     *
     * @param str   源数,字符串格式
     * @param scale 精度
     * @return 四舍五入后的数
     */
    public static String decimal(String str, int scale)
    {
        double value = getDouble(str);
        return decimal(value, scale);
    }

    /**
     * 获取四舍五入后的数字
     *
     * @param value 源数,double
     * @param scale 精度
     * @return 四舍五入后的数
     */
    public static String decimal(Double value, int scale)
    {
        String format = "#0";
        for (int i = 0; i < scale; i++)
        {
            format += (i == 0 ? "." : "") + "0";
        }
        return format(value, format);
    }

    /**
     * 格式化数字
     *
     * @param value   数字
     * @param pattern 格式字符串
     * @return 格式化后的字符串
     */
    public static String format(Long value, String pattern)
    {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(value);
    }

    /**
     * 格式化数字
     *
     * @param value   数字
     * @param pattern 格式字符串
     * @return 格式化后的字符串
     */
    public static String format(Integer value, String pattern)
    {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(value);
    }

    /**
     * 格式化数字
     *
     * @param value   数字
     * @param pattern 格式字符串
     * @return 格式化后的字符串
     */
    public static String format(Double value, String pattern)
    {
        DecimalFormat df = new DecimalFormat(pattern);
        return df.format(value);
    }

    /**
     * 通过类名获取编译的class所在绝对路径
     *
     * @param clzName 类名
     * @return 类class所在绝对路径
     */
    public static String getFilePathByClassName(String clzName)
    {
        return SystemUtil.getClassesPath() + clzName.replaceAll("\\.", "/") + ".class";
    }

    public static String lpad(String src, char c, int length)
    {
        if (src.length() >= length)
        {
            return src;
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < length - src.length(); i++)
            {
                sb.append(c);
            }
            sb.append(src);
            return sb.toString();
        }
    }

    public static void appendXmlElement(StringBuilder sb, String eleName, String eleValue)
    {
        sb.append("<").append(eleName).append(">").append(eleValue).append("");
    }

    public static int getWordLength(String word)
    {
        if (StringUtil.isNull(word))
        {
            return 0;
        }
        return word.replaceAll("[^\\x00-\\xff]", "**").length();
    }

    @SuppressWarnings("unchecked")
    public static String toString(Object obj)
    {
        if (null == obj)
        {
            return null;
        }
        else if (ClassUtil.isString(obj))
        {
            return (String) obj;
        }
        else if (ClassUtil.isInteger(obj) || ClassUtil.isFloat(obj) || ClassUtil.isDouble(obj))
        {
            return obj + "";
        }
        else if (ClassUtil.isDate(obj))
        {
            return DateUtil.getFormat((Date) obj, DateFormatConst.YMDHMS_);
        }
        else if (ClassUtil.isArray(obj))
        {
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            Object[] arr = (Object[]) obj;
            for (int i = 0; i < arr.length; i++)
            {
                sb.append(0 == i ? "" : ",").append(toString(arr[i]));
            }
            sb.append("]");
            return sb.toString();
        }
        else if (ClassUtil.isList(obj))
        {
            StringBuilder sb = new StringBuilder();
            sb.append("[");
            List list = (List) obj;
            for (int i = 0; i < list.size(); i++)
            {
                sb.append(0 == i ? "" : ",").append(toString(list.get(i)));
            }
            sb.append("]");
            return sb.toString();
        }
        else if (ClassUtil.isMap(obj))
        {
            StringBuilder sb = new StringBuilder();
            sb.append("{");
            Map map = (Map) obj;
            Iterator it = map.keySet().iterator();
            int count = 0;
            while (it.hasNext())
            {
                Object key = it.next();
                if (0 != count++)
                {
                    sb.append(0 == count++ ? "" : ",");
                }
                sb.append("\"").append(key).append("\":\"").append(toString(map.get(key))).append("\"");
            }
            sb.append("}");
            return sb.toString();
        }
        else
        {
            return obj.toString();
        }
    }
}