Here you can find the source of lpad(String str, int size)
Parameter | Description |
---|---|
str | the string to be padded |
size | pad to this size |
public static String lpad(String str, int size)
//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; } }