Java String Pad Left leftPad(String in, char padding, int length)

Here you can find the source of leftPad(String in, char padding, int length)

Description

left Pad

License

Open Source License

Declaration

public static String leftPad(String in, char padding, int length) 

Method Source Code

//package com.java2s;
/*/*from ww  w .j av  a2 s  .  c  o m*/
GPON General Purpose Object Network
Copyright (C) 2006 Daniel Schulz
    
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 {
    public static String leftPad(String in, char padding, int length) {

        String out = "";

        int toPad = length - in.length();

        if (toPad < 0) {
            throw new IllegalArgumentException("Input string longer than requested length");
        }

        out = getStringRepetition(padding + "", toPad) + in;

        return out;
    }

    public static String getStringRepetition(String in, int times) {
        StringBuffer out = new StringBuffer("");

        for (int i = 0; i < times; i++) {
            out = out.append(in);
        }

        return out.toString();
    }
}

Related

  1. leftPad(Object obj, char pad, int len)
  2. leftPad(String _str, int _size, char _padChar)
  3. leftPad(String aStr, int aLen)
  4. leftPad(String base, int length, char pad)
  5. leftPad(String csIn, int nRequiredLength, char cFill)
  6. leftPad(String input, char padding, int length)
  7. leftPad(String input, int length, char pad)
  8. leftPad(String inStr, int length, char paddingChar)
  9. leftPad(String original, int length, char padChar)