[Java]常用的字串String函數function

字串或數字不足碼部份補零

/**
     * 數字不足部份補零回傳
     * @param str 數字
     * @param lenSize 數字最大長度,不足的部份補零
     * @return 回傳補零後字串數字
     */
    public static String MakesUpZero(int str, int lenSize) {
        return MakesUpZero(String.valueOf(str), lenSize);
    }

    /**
     * 數字不足部份補零回傳
     * @param str 字串
     * @param lenSize 字串數字最大長度,不足的部份補零
     * @return 回傳補零後字串數字
     */
    public static String MakesUpZero(String str, int lenSize) {
        String zero = "0000000000";
        String returnValue = zero;

        returnValue = zero + str;

        return returnValue.substring(returnValue.length() - lenSize);

    }
    public static void main(String args[])
    {
        //把字串123前方不足五碼的部份補零
        System.out.println("不足5碼部份補零:"+StringUtils.MakesUpZero("123", 5));
        //把數字123前方不足六碼的部份補零
        System.out.println("不足6碼部份補零:"+StringUtils.MakesUpZero(123, 6));
    }

字串判斷是否為空白,及字串、數字nvl

/**
     * 檢查字串是否為null或空白
     * @param value 傳入字串
     * @return 為空白或null則回傳true,否則則回傳false
     */
    public static boolean isEmpty(String value) {
        if (value == null || value.trim().equals(""))
            return true;
        else
            return false;
    }

    /**
     * 字串如果為空白或null則使用def取代
     * @param value 檢查之字串
     * @param def 如為空白或null 要取代的值
     * @return 回傳value或是def值
     */
    public static String nvl(String value, String def) {
        if (isEmpty(value))
            return def;
        else
            return value.trim();
    }

    /**
     * 字串為數字格式則轉換成數字回傳,否則則用預設值取代
     * @param str 要檢查的字串
     * @param def 不為數字時要回傳的預設值
     * @return 字串轉換成數字值
     */
    public static int intNvl(String str, int def) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
            return def;
        }

    }

    /**
     * 字串為數字格式則轉換成數字BigDecimal回傳,否則則用預設值取代
     * @param str 要檢查的字串
     * @param def 不為數字時要回傳的預設值BigDecimal
     * @return 字串轉換成數字值BigDecimal
     */
    public static BigDecimal BigDecimalNvl(String str, BigDecimal def) {
        try {
            return new BigDecimal(str);
        } catch (Exception e) {
            return def;
        }

    }
    public static void main(String args[])
    {
        //檢查字串否為空白檢
        System.out.println("是否為空白:"+StringUtils.isEmpty(""));
        System.out.println("A是否為空白:"+StringUtils.isEmpty("A"));
        System.out.println(" 是否為空白:"+StringUtils.isEmpty(" "));
        System.out.println("-------------------------------");
        //字串nvl
        System.out.println("空白 :"+StringUtils.nvl("", "啦啦啦"));
        System.out.println("非空白:"+StringUtils.nvl("AAA", "啦啦啦"));
        System.out.println("-------------------------------");
        
        //字串是否為數字
        System.out.println("123數字:"+StringUtils.intNvl("123", 0));
        System.out.println("A123數字:"+StringUtils.intNvl("A123", 0));
        System.out.println("-------------------------------");
        
        //字串是否為數字BigDecimal
        System.out.println("BigDecimal:");
        System.out.println("123數字:"+StringUtils.BigDecimalNvl("123", BigDecimal.ZERO));
        System.out.println("A123數字:"+StringUtils.BigDecimalNvl("A123", BigDecimal.ZERO));
        System.out.println("-------------------------------");
    }

發表迴響