Android String Format fillSpace(String formatString, int length)

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

Description

Method fill string

License

Open Source License

Parameter

Parameter Description
The string to be format.

Declaration

public static String fillSpace(String formatString, int length) 

Method Source Code

//package com.java2s;

public class Main {
    /**//ww  w .j  av  a 2s.  com
     * Method fill string
     *
     * @param The string to be format.
     *
     */
    public static String fillSpace(String formatString, int length) {
        return fillString(formatString, length, ' ', false);
    }

    /**
     * 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. formatNumberWithTwoDecimals(Locale locale, String stringToFormat)
  2. formatPriceWithThreeDecimals(Locale locale, String stringToFormat)
  3. formatVolume(Locale locale, String stringToFormat)
  4. formatPriceWithTwoDecimals(Locale locale, String stringToFormat)
  5. format(String formatString, Object... args)
  6. fillString(String formatString, int length, char fillChar, boolean leftFillFlag)
  7. fillZero(String formatString, int length)
  8. format(String message, Object[] params)
  9. centerString(String input, int size)