Java String Pad Left lpad(int value, int padLen, char padChar)

Here you can find the source of lpad(int value, int padLen, char padChar)

Description

Returns value argument, left-padded to length padLen argument with the sequence of character in padChar argument.

License

Open Source License

Parameter

Parameter Description
value a int value.
padLen the total length of the return value.
padChar padded character.

Return

left padded string.

Declaration

public static String lpad(int value, int padLen, char padChar) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  ww  w.  jav  a 2s  .  c o  m*/
     * Returns value argument, left-padded to length padLen argument with the
     * sequence of character in padChar argument.
     * 
     * If the value argument is null, value argument think of empty string
     * ("").
     * 
     * @param value
     *            a string value.
     * @param padLen
     *            the total length of the return value.
     * @param padChar
     *            padded character.
     * @return left padded string.
     */
    public static String lpad(String value, int padLen, char padChar) {
        if (value == null)
            value = "";

        while (value.length() < padLen) {
            value = padChar + value;
        }

        return value;
    }

    /**
     * Returns value argument, left-padded to length padLen argument with the
     * sequence of character in padChar argument.
     * 
     * @param value
     *            a short value.
     * @param padLen
     *            the total length of the return value.
     * @param padChar
     *            padded character.
     * @return left padded string.
     */
    public static String lpad(short value, int padLen, char padChar) {
        return lpad(String.valueOf(value), padLen, padChar);
    }

    /**
     * Returns value argument, left-padded to length padLen argument with the
     * sequence of character in padChar argument.
     * 
     * @param value
     *            a int value.
     * @param padLen
     *            the total length of the return value.
     * @param padChar
     *            padded character.
     * @return left padded string.
     */
    public static String lpad(int value, int padLen, char padChar) {
        return lpad(String.valueOf(value), padLen, padChar);
    }

    /**
     * Returns value argument, left-padded to length padLen argument with the
     * sequence of character in padChar argument.
     * 
     * @param value
     *            a long value.
     * @param padLen
     *            the total length of the return value.
     * @param padChar
     *            padded character.
     * @return left padded string.
     */
    public static String lpad(long value, int padLen, char padChar) {
        return lpad(String.valueOf(value), padLen, padChar);
    }

    /**
     * Returns value argument, left-padded to length padLen argument with the
     * sequence of character in padChar argument.
     * 
     * @param value
     *            a float value.
     * @param padLen
     *            the total length of the return value.
     * @param padChar
     *            padded character.
     * @return left padded string.
     */
    public static String lpad(float value, int padLen, char padChar) {
        return lpad(String.valueOf(value), padLen, padChar);
    }

    /**
     * Returns value argument, left-padded to length padLen argument with the
     * sequence of character in padChar argument.
     * 
     * @param value
     *            a double value.
     * @param padLen
     *            the total length of the return value.
     * @param padChar
     *            padded character.
     * @return left padded string.
     */
    public static String lpad(double value, int padLen, char padChar) {
        return lpad(String.valueOf(value), padLen, padChar);
    }
}

Related

  1. lpad(final String input, final String padCode, final int toLength)
  2. lpad(final String s, final String pad, final int size)
  3. lpad(int i, int length, char c)
  4. lpad(int length, long number)
  5. lpad(int maxDigits, long val)
  6. lpad(Object str, int len, String padding)
  7. lpad(String base, int len, String pad)
  8. lpad(String input, char padding, int length)
  9. lpad(String input, int length)