Android String Format fillZero(String formatString, int length)

Here you can find the source of fillZero(String formatString, int length)

Description

Method Format string

License

Open Source License

Parameter

Parameter Description
The string to be format.

Declaration

public static String fillZero(String formatString, int length) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from  w ww. j a v a2  s. c  o m
     * Method Format string
     *
     * @param The string to be format.
     *
     */
    public static String fillZero(String formatString, int length) {
        return fillString(formatString, length, '0', true);
    }

    /**
     * Method fill string
     *
     * @param The string to be format.
     *
     */
    public static String fillString(String formatString, int length,
            char fillChar, boolean leftFillFlag) {
        if (null == formatString) {
            formatString = "";
        }
        int strLen = formatString.length();
        if (strLen >= length) {
            if (true == leftFillFlag) // left fill 
                return formatString.substring(strLen - length, strLen);
            else
                return formatString.substring(0, length);
        } else {
            StringBuffer sbuf = new StringBuffer();
            int fillLen = length - formatString.length();
            for (int i = 0; i < fillLen; i++) {
                sbuf.append(fillChar);
            }

            if (true == leftFillFlag) // left fill 
            {
                sbuf.append(formatString);
            } else {
                sbuf.insert(0, formatString);
            }
            String returnString = sbuf.toString();
            sbuf = null;
            return returnString;
        }
    }

    /**
     * Method convert byte[] to String
     *
     * @param The string to be format.
     *
     */
    public static String toString(byte[] buffer) {
        if (null == buffer)
            return null;
        else
            return new String(buffer);
    }
}

Related

  1. formatVolume(Locale locale, String stringToFormat)
  2. formatPriceWithTwoDecimals(Locale locale, String stringToFormat)
  3. format(String formatString, Object... args)
  4. fillSpace(String formatString, int length)
  5. fillString(String formatString, int length, char fillChar, boolean leftFillFlag)
  6. format(String message, Object[] params)
  7. centerString(String input, int size)