Java String Pad Left leftPad(String _str, int _size, char _padChar)

Here you can find the source of leftPad(String _str, int _size, char _padChar)

Description

left Pad

License

Open Source License

Declaration

public static String leftPad(String _str, int _size, char _padChar) 

Method Source Code

//package com.java2s;

public class Main {

    public static String leftPad(String _str, int _size, char _padChar) {
        if (_str == null) {
            return null;
        }/*from www.j a  v  a2s  . c om*/
        int pads = _size - _str.length();
        if (pads <= 0) {
            return _str; // returns original String when possible
        }
        return padding(pads, _padChar).concat(_str);
    }

    private static String padding(int _repeat, char _padChar) {
        String value = "";
        String padStr = String.valueOf(_padChar);
        if (_repeat > 0) {
            for (int i = 0; i < _repeat; i++) {
                value = value.concat(padStr);
            }
        }
        return value;
    }
}

Related

  1. leftPad(final String numStr, final int numDigits)
  2. leftPad(final String str, final int size, String padStr)
  3. leftPad(final String value, final int length, final char paddingChar)
  4. leftPad(long value, char padChar, int maxDigits, StringBuilder buf)
  5. leftPad(Object obj, char pad, int len)
  6. leftPad(String aStr, int aLen)
  7. leftPad(String base, int length, char pad)
  8. leftPad(String csIn, int nRequiredLength, char cFill)
  9. leftPad(String in, char padding, int length)