Java String Pad Left lpad(String s, int len, char ch)

Here you can find the source of lpad(String s, int len, char ch)

Description

Pads a string to the desired length.

License

Open Source License

Parameter

Parameter Description
s the string.
len the desired length.
ch the character used for padding

Return

the padded string.

Declaration

public static String lpad(String s, int len, char ch) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  www . j a v a 2  s .  co  m*/
     * Pads a string to the desired length.
     * @param s the string.
     * @param len the desired length.
     * @param ch the character used for padding
     * @return the padded string.
     */
    public static String lpad(String s, int len, char ch) {
        if (s == null || s.length() >= len)
            return s;

        StringBuffer buf = new StringBuffer(len);
        for (len -= s.length(); len > 0; len--)
            buf.append(ch);

        buf.append(s);
        return buf.toString();
    }
}

Related

  1. lpad(String input, char padding, int length)
  2. lpad(String input, int length)
  3. lpad(String input, String padChar, int finalLength)
  4. lPad(String input, String replace, int length)
  5. lpad(String s, char addChar, int length)
  6. lpad(String s, int len, String padc)
  7. lpad(String s, int length, char pad)
  8. lpad(String s, int width)
  9. lpad(String s, String fill, int len)