Java Utililty Methods String Pad Left

List of utility methods to do String Pad Left

Description

The list of methods to do String Pad Left are organized into topic(s).

Method

StringleftPadString(String sValue, int iLength, String sPadString)
Left pad the string with the specific character and maximum length.
String sZeros = "";
String sResult = "";
int iRemainLength = 0;
iRemainLength = iLength - sValue.length();
if (iRemainLength != 0) {
    while (iRemainLength-- > 0) {
        sZeros += sPadString;
sResult = sZeros + sValue;
return sResult;
StringleftPadWithZeros(String input, int expectedSize)
left Pad With Zeros
if (input == null) {
    return leftPadWithZeros("", expectedSize);
StringBuilder sb = new StringBuilder(expectedSize);
for (int i = expectedSize - input.length(); i > 0; i--) {
    sb.append(ZERO);
sb.append(input);
...
voidleftPadZeros(int value, int digits, StringBuilder sb)
left Pad Zeros
sb.append(String.format("%0" + digits + "d", value));
StringleftZeroPad(String s)
left Zero Pad
if (s.length() < 16) {
    String padding = "0000000000000000".substring(0, 16 - s.length());
    s = padding + s;
if (s.length() != 16)
    throw new IllegalStateException("Generated result of '" + s + "'");
return s;
StringleftZeroPadding(int number, int howManyChar)
Fill at the left with zeros...
return String.format("%0" + howManyChar + "d", number);
Stringlpad(final String input, final String padCode, final int toLength)
lpad
if (input != null && input.length() < toLength) {
    final StringBuffer sb = new StringBuffer(input);
    while (sb.length() < toLength) {
        sb.insert(0, padCode);
    return sb.toString();
} else {
    return input;
...
Stringlpad(final String s, final String pad, final int size)
add characters on the left until the string get as long as the specified size
int missing = size - s.length();
StringBuffer sb = new StringBuffer();
if (missing > 0) {
    int len = sb.length();
    while (len < missing) {
        String ppad = cut(pad, missing - len);
        sb.append(ppad);
        missing -= ppad.length();
...
Stringlpad(int i, int length, char c)
Left pad an integer to a given length with the given character
StringBuffer buf = new StringBuffer(String.valueOf(i));
while (buf.length() < length) {
    buf.insert(0, c);
return buf.toString();
Stringlpad(int length, long number)
lpad
String f = "%0" + length + "d";
return String.format(f, number);
Stringlpad(int maxDigits, long val)
lpad
int ndigits = ndigits(val);
int pad = maxDigits - ndigits;
StringBuilder buf = new StringBuilder();
while (pad > 0) {
    buf.append('0');
    pad--;
buf.append(val);
...