Java String Pad Left leftPad(String csIn, int nRequiredLength, char cFill)

Here you can find the source of leftPad(String csIn, int nRequiredLength, char cFill)

Description

left Pad

License

LGPL

Declaration

public static String leftPad(String csIn, int nRequiredLength, char cFill) 

Method Source Code

//package com.java2s;
/*//from   w w  w  .  j  a v  a 2s  . co  m
 * JLib - Publicitas Java library v1.2.0.
 *
 * Copyright (c) 2005, 2006, 2007, 2008, 2009 Publicitas SA.
 * Licensed under LGPL (LGPL-LICENSE.txt) license.
 */

public class Main {
    public static String leftPad(String csIn, int nRequiredLength, char cFill) {
        String csOut = new String();
        int nLgStringIn = csIn.length();
        int nNbcharToPad = nRequiredLength - nLgStringIn;
        if (nNbcharToPad > 0) {
            for (int n = 0; n < nNbcharToPad; n++) {
                csOut = csOut + cFill;
            }
            csOut = csOut + csIn;
        } else // Keep only leftmost chars
        {
            csOut = csIn.substring(0, nRequiredLength);
        }

        return csOut;
    }
}

Related

  1. leftPad(long value, char padChar, int maxDigits, StringBuilder buf)
  2. leftPad(Object obj, char pad, int len)
  3. leftPad(String _str, int _size, char _padChar)
  4. leftPad(String aStr, int aLen)
  5. leftPad(String base, int length, char pad)
  6. leftPad(String in, char padding, int length)
  7. leftPad(String input, char padding, int length)
  8. leftPad(String input, int length, char pad)
  9. leftPad(String inStr, int length, char paddingChar)