Java String Pad Left lPad(String str, int length, String padString)

Here you can find the source of lPad(String str, int length, String padString)

Description

To pad the given string with a user specified character on the left up to the given length.

License

Apache License

Parameter

Parameter Description
str String to be padded
length he required length of the resulted string.
padString The required padding string

Return

The padded string

Declaration

public static String lPad(String str, int length, String padString) 

Method Source Code

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

public class Main {
    /**/*from  w w w .  ja va  2  s . c  o m*/
     * To pad the given string with a user specified character on the left up to
     * the given length. e.g. lPad("ABCD", 10, 'X') returns "XXXXXXABCD" which
     * has a length of 10. This method has built-in 'intelligence' to handle
     * cases where calling method If <I>str</I> already longer than
     * <I>length</I>, return <I>str</I> itself. tries to be funny and supply the
     * following : - lPad("abc", 10, "123") it will return, "1231231abc"
     * 
     * @param str String to be padded
     * @param length he required length of the resulted string.
     * @param padString The required padding string
     * @return The padded string
     */
    public static String lPad(String str, int length, String padString) {
        int lOriginal = str.length();
        int lPadStr = padString.length();
        int times2Pad = 0;
        int lPadded = 0;

        if (lOriginal >= length)
            return str;

        StringBuffer sb = new StringBuffer();
        String padded;

        times2Pad = (length - lOriginal) / lPadStr; // will give (1) if 3/2

        padded = duplicate(padString, times2Pad);
        lPadded = padded.length();
        sb.append(padded); // pad in the repetitive characters

        // if still insufficient by the modulus e.g. 30/20 is 10
        if (lOriginal + lPadded < length) {
            int more = length - (lOriginal + lPadded);

            // add in the difference which is less entire length of padStr
            sb.append(padString.substring(0, more));
        }

        sb.append(str); // pad the original string behind

        return sb.toString();
    }

    /**
     * Pads the string with prevailing spaces.
     * 
     * @param str String to be padded with spaces on the left.
     * @param len The number of spaces to pad to the left of the string.
     * @return The space-padded string.
     */
    public static String lPad(String str, int len) {
        return lPad(str, len, " ");
    }

    /**
     * To return a string which is filled with a specified string. e.g.
     * duplicate("*", 5) returns "*****", duplicate("OK", 3) returns "OKOKOK"
     * repeated for given number of times
     * 
     * @param str String to be repeated/duplicated
     * @param times Number of time the string to be repeated/duplicated
     * @return The resulted string with <code>str</code> repeated the specified
     *         number of times.
     */
    public static String duplicate(String str, int times) {
        StringBuffer result = new StringBuffer();

        for (int i = 0; i < times; i++) {
            result.append(str);
        }
        return (result.toString());
    }
}

Related

  1. lpad(String src, int length, char padding)
  2. lpad(String str, char pad, int len)
  3. lPad(String str, int len, char pad)
  4. lpad(String str, int len, char padding)
  5. LPad(String str, int length, String chr)
  6. lpad(String str, int size)
  7. lpad(String str, int totalPadAmount, String padChar)
  8. lpad(String str, String chr, int length)
  9. lPad(String string, char[] padding, int length)