Here you can find the source of leftPad(String original, int length, char padChar)
public static String leftPad(String original, int length, char padChar)
//package com.java2s; /*//ww w. j av a 2 s. c o m * Copyright (c) 2016 Vivid Solutions. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * * http://www.eclipse.org/org/documents/edl-v10.php. */ public class Main { /** * Pads the String with the given character until it has the given length. If * original is longer than the given length, returns original. */ public static String leftPad(String original, int length, char padChar) { if (original.length() >= length) { return original; } return stringOfChar(padChar, length - original.length()) + original; } /** * Returns a String of the given length consisting entirely of the given * character */ public static String stringOfChar(char ch, int count) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < count; i++) { buf.append(ch); } return buf.toString(); } }