Java String Pad Left lpad(String input, String padChar, int finalLength)

Here you can find the source of lpad(String input, String padChar, int finalLength)

Description

lpad

License

Open Source License

Declaration

public static String lpad(String input, String padChar, int finalLength) 

Method Source Code

//package com.java2s;
/*//from  ww  w .  j  a v  a2 s  .  c o m
 * Keystone Development Framework
 * Copyright (C) 2004-2009 Rick Knowles
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * Version 2 as published by the Free Software Foundation.
 *
 * This program 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 General Public License Version 2 for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * Version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

public class Main {
    public static String lpad(String input, String padChar, int finalLength) {
        StringBuffer out = new StringBuffer(input);

        int currentLength = out.length();
        while (currentLength < finalLength) {
            out.insert(0, padChar.subSequence(0, Math.min(padChar.length(), finalLength - currentLength)));
            currentLength = out.length();
        }

        return out.toString();
    }
}

Related

  1. lpad(int value, int padLen, char padChar)
  2. lpad(Object str, int len, String padding)
  3. lpad(String base, int len, String pad)
  4. lpad(String input, char padding, int length)
  5. lpad(String input, int length)
  6. lPad(String input, String replace, int length)
  7. lpad(String s, char addChar, int length)
  8. lpad(String s, int len, char ch)
  9. lpad(String s, int len, String padc)