Java String Pad Left leftPad(String aStr, int aLen)

Here you can find the source of leftPad(String aStr, int aLen)

Description

Right-justify a String in a field, padding left with blank.

License

LGPL

Parameter

Parameter Description
aStr the string
aLen length of field

Return

the right-justified string

Declaration

public static String leftPad(String aStr, int aLen) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    /**/*from ww  w.j  a va  2 s .com*/
     *  Right-justify a String in a field, padding left with blank.
     *
     * @param  aStr  the string
     * @param  aLen  length of field
     * @return       the right-justified string
     */
    public static String leftPad(String aStr, int aLen) {
        return replicate(' ', aLen - aStr.length()) + aStr;
    }

    /**
     *  Right-justify each String uniformly in in an Array of String,
     * padding left with blank.
     *
     * @param  aLines  the Strings to justify
     * @param  aLen    uniform field length of output strings
     * @return         Array of right-justified string
     */
    public static String[] leftPad(String aLines[], int aLen) {
        for (int idx = 0; idx < aLines.length; idx++) {
            aLines[idx] = leftPad(aLines[idx], aLen);
        }

        return aLines;
    }

    /**
     *  Create a String filled with a single character
     *
     * @param  aChar  the character to fill with
     * @param  aLen   how many to fill with
     * @return        A string consisting of the char repeated aLen times
     */
    public static String replicate(char aChar, int aLen) {
        StringBuffer mBuf = new StringBuffer(aLen);
        for (int mIdx = 0; mIdx < aLen; mIdx++) {
            mBuf.append(aChar);
        }
        return mBuf.toString();
    }
}

Related

  1. leftPad(final String str, final int size, String padStr)
  2. leftPad(final String value, final int length, final char paddingChar)
  3. leftPad(long value, char padChar, int maxDigits, StringBuilder buf)
  4. leftPad(Object obj, char pad, int len)
  5. leftPad(String _str, int _size, char _padChar)
  6. leftPad(String base, int length, char pad)
  7. leftPad(String csIn, int nRequiredLength, char cFill)
  8. leftPad(String in, char padding, int length)
  9. leftPad(String input, char padding, int length)