Java String Pad Left leftPad(String srcStr, char padChar, int destLen)

Here you can find the source of leftPad(String srcStr, char padChar, int destLen)

Description

left Pad

License

Open Source License

Declaration

public final static String leftPad(String srcStr, char padChar, int destLen) 

Method Source Code

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

public class Main {

    public final static String leftPad(String srcStr, char padChar, int destLen) {

        if (srcStr == null) {
            return genStr(padChar, destLen);
        } else if (srcStr.length() < destLen) {
            StringBuilder destSrc = new StringBuilder(destLen);
            destSrc.append(srcStr);/*from   w ww. j a v  a 2 s.  co  m*/
            for (int i = srcStr.length(); i < destLen; i++) {
                destSrc.insert(0, padChar);
            }
            return destSrc.toString();
        } else {
            return srcStr;
        }
    }

    private static String genStr(char padChar, int len) {
        StringBuilder str = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            str.append(padChar);
        }
        return str.toString();
    }
}

Related

  1. leftPad(String s, int minLength)
  2. leftPad(String s, int n)
  3. leftPad(String s, int target)
  4. leftPad(String s, int width)
  5. leftPad(String s, int z)
  6. leftPad(String srcString, char c, int length)
  7. leftPad(String srcString, char c, int length)
  8. leftPad(String str, int len, char c)
  9. leftPad(String str, int length)