Java String Pad Left lpad(String str, int size)

Here you can find the source of lpad(String str, int size)

Description

lpad

License

Open Source License

Parameter

Parameter Description
str the string to be padded
size pad to this size

Return

the supplied string padded to the left with spaces.

Declaration

public static String lpad(String str, int size) 

Method Source Code

//package com.java2s;
/*/*w w  w . j  a v a 2 s  .  co m*/
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

public class Main {
    /**
     * @param str the string to be padded
     * @param size pad to this size
     * @return the supplied string padded to the left with spaces.
     */
    public static String lpad(String str, int size) {
        if (str.length() >= size)
            return str;

        String pad = "";
        int nbBlanks = size - str.length();
        for (int i = 0; i < nbBlanks; i++)
            pad += " ";

        return pad + str;
    }
}

Related

  1. lpad(String str, char pad, int len)
  2. lPad(String str, int len, char pad)
  3. lpad(String str, int len, char padding)
  4. LPad(String str, int length, String chr)
  5. lPad(String str, int length, String padString)
  6. lpad(String str, int totalPadAmount, String padChar)
  7. lpad(String str, String chr, int length)
  8. lPad(String string, char[] padding, int length)
  9. lPad(String target, String fix, int length)