Java String Pad Left leftPad(String text, int length, char padChar)

Here you can find the source of leftPad(String text, int length, char padChar)

Description

left Pad

License

Open Source License

Declaration

public static String leftPad(String text, int length, char padChar) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004-2010 Sunil Kamath (IcemanK).
 * All rights reserved./*from   w ww .  ja v a  2 s  . c o m*/
 * This program is made available under the terms of the Common Public License
 * v1.0 which is available at http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors:
 *     Sunil Kamath (IcemanK) - initial API and implementation
 *
 *******************************************************************************/

public class Main {
    public static String leftPad(String text, int length, char padChar) {
        String text2 = text;
        if (text2.length() < length) {
            StringBuffer buf = new StringBuffer(""); //$NON-NLS-1$
            for (int i = text2.length(); i < length; i++) {
                buf.append(padChar);
            }
            buf.append(text2);
            text2 = buf.toString();
        }
        return text2;
    }

    public static String toString(Object object, String defaultValue) {
        if (object == null) {
            return defaultValue;
        } else {
            return object.toString();
        }
    }
}

Related

  1. leftPad(String str, String character, int size)
  2. leftPad(String string, char pad, int size)
  3. leftPad(String string, int length)
  4. leftPad(String strInput, int intLength)
  5. leftPad(String targetStr, char appendChar, int length)
  6. leftPad(String text, int size)
  7. leftPad(String toPad, int numPads)
  8. leftPad(String value, int makeLength, char paddingCharacter)
  9. leftPad(String value, int size, String pad)