Java String Pad Left leftPad(String input, char padding, int length)

Here you can find the source of leftPad(String input, char padding, int length)

Description

left Pad

License

LGPL

Declaration

public static String leftPad(String input, char padding, int length) 

Method Source Code

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

public class Main {
    public static String leftPad(String input, char padding, int length) {
        if (nullOrEmpty(input))
            input = "";
        if (input.length() >= length)
            return input;
        String padString = createPad(padding, length - input.length());
        return padString.concat(input);
    }//from  w  w  w.j a  va  2 s.  co  m

    public static boolean nullOrEmpty(String s) {
        return (s == null) ? true : "".equals(s.trim());
    }

    public static String createPad(char padding, int length) {
        StringBuilder out = new StringBuilder();
        for (int repeat = 0; repeat < length; repeat++)
            out.append(padding);
        return out.toString();
    }

    public static String trim(String s) {
        return emptyIfNull(s).trim();
    }

    public static String emptyIfNull(String s) {
        return (s == null) ? "" : s;
    }
}

Related

  1. leftPad(String _str, int _size, char _padChar)
  2. leftPad(String aStr, int aLen)
  3. leftPad(String base, int length, char pad)
  4. leftPad(String csIn, int nRequiredLength, char cFill)
  5. leftPad(String in, char padding, int length)
  6. leftPad(String input, int length, char pad)
  7. leftPad(String inStr, int length, char paddingChar)
  8. leftPad(String original, int length, char padChar)
  9. leftPad(String s, char paddingCharacter, int length)