Here you can find the source of leftPad(String srcStr, char padChar, int destLen)
public final static String leftPad(String srcStr, char padChar, int destLen)
//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(); } }