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

StringBuilderleftPad(StringBuilder pStringBuilder, int pLength, char pChar)
Similar to the Oracle lpad command.
StringBuilder lTempBuffer = new StringBuilder(pLength);
while ((lTempBuffer.length() + pStringBuilder.length()) < pLength) {
    lTempBuffer.append(pChar);
lTempBuffer.append(pStringBuilder);
return lTempBuffer;
byte[]leftPadByteArray(byte[] source, int size)
Left pad byte array to a byte array with given size.
byte[] bs = new byte[size];
if (size < source.length) {
    return null;
int startPos = size - source.length;
System.arraycopy(source, 0, bs, startPos, source.length);
return bs;
StringleftPadded(String src, int len)
Pad the src to make it [len] bytes with trailing spaces.
return leftPadded(src, len, ' ');
StringleftPaddedString(String string, int length, char paddingChar)
left Padded String
if (string.length() > length) {
    System.err.println("WARNING: String to pad is longer than expected length. Return original string.");
    return string;
if (string.length() == length)
    return string;
return String.format("%0" + (length - string.length()) + "d", 0).replace('0', paddingChar).concat(string);
StringBufferleftPadding(final StringBuffer strBuf, final int bufLen, final char fill)
Left Pad Stringbuffer with special char
while (strBuf.length() < bufLen)
    strBuf.insert(0, fill);
return strBuf;
StringleftPadding(String in, int count, char pad)
Applies a left padding to the provided string.
if (in.length() >= count)
    return in;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < count - in.length(); i++) {
    sb.append(pad);
for (char c : in.toCharArray()) {
    sb.append(c);
...
StringleftPadding(String orgStr, String addStr, int strLength)
left Padding
if (orgStr.length() < strLength) {
    String padding = new String();
    int len = strLength - orgStr.length();
    for (int loop = 0; loop < len; loop++) {
        padding = padding + addStr;
    padding = padding + orgStr;
    return padding;
...
StringleftPadInt(int number, int width)
Left pads number with zero's
return leftPad(Integer.toString(number), width, '0');
StringleftPadMultiline(String input, char padChar, int padWidth)
left Pad Multiline
StringBuilder buf = new StringBuilder(input);
String paddingString = repeatedChar(padChar, padWidth);
int i = 0;
while (i < buf.length()) {
    if (buf.charAt(i) == '\n') {
        buf.insert(i + 1, paddingString);
    i++;
...
StringleftPadString(String str, char pad, int length)
left Pad String
if (str.length() >= length)
    return str;
StringBuffer sb = new StringBuffer();
while (sb.length() < length - str.length())
    sb.append(pad);
sb.append(str);
return sb.toString();