Here you can find the source of leftPad(String toPad, int numPads)
public static String leftPad(String toPad, int numPads)
//package com.java2s; //License from project: Open Source License public class Main { public static String leftPad(String toPad, int numPads) { return leftPad(toPad, numPads, " "); }/* w w w . j a va 2s . com*/ public static String leftPad(String toPad, int padLength, String padString) { if (toPad == null || toPad.length() == 0) return toPad; if (padLength < 1) return ""; int stringToPadLength = toPad.length(); // if pad length < toPad's length, truncate toPad string if (stringToPadLength > padLength) { return toPad.substring(0, padLength); } // if pad length = toPad's length, return toPad else if (stringToPadLength == padLength) { return toPad; } // build padding until amount to pad is met (or exceeded) int numCharsToPad = padLength - stringToPadLength; String padding = ""; while (padding.length() < numCharsToPad) { padding += padString; } // if padding exceeds pad length, trim it if (padding.length() > numCharsToPad) { padding = padding.substring(0, numCharsToPad); } return padding + toPad; } }