Logo Search packages:      
Sourcecode: libcommons-lang-java version File versions  Download package

static String org::apache::commons::lang::StringUtils::leftPad ( String  str,
int  size,
String  padStr 
) [inline, static]

Left pad a String with a specified String.

Pad to a size of size.

 StringUtils.leftPad(null, *, *)      = null
 StringUtils.leftPad("", 3, "z")      = "zzz"
 StringUtils.leftPad("bat", 3, "yz")  = "bat"
 StringUtils.leftPad("bat", 5, "yz")  = "yzbat"
 StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat"
 StringUtils.leftPad("bat", 1, "yz")  = "bat"
 StringUtils.leftPad("bat", -1, "yz") = "bat"
 StringUtils.leftPad("bat", 5, null)  = "  bat"
 StringUtils.leftPad("bat", 5, "")    = "  bat"
 

Parameters:
str the String to pad out, may be null
size the size to pad to
padStr the String to pad with, null or empty treated as single space
Returns:
left padded String or original String if no padding is necessary, null if null String input

Definition at line 3268 of file StringUtils.java.

References leftPad(), PAD_LIMIT, and padding().

                                                                      {
        if (str == null) {
            return null;
        }
        if (padStr == null || padStr.length() == 0) {
            padStr = " ";
        }
        int padLen = padStr.length();
        int strLen = str.length();
        int pads = size - strLen;
        if (pads <= 0) {
            return str; // returns original String when possible
        }
        if (padLen == 1 && pads <= PAD_LIMIT) {
            return leftPad(str, size, padStr.charAt(0));
        }
        
        if (pads == padLen) {
            return padStr.concat(str);
        } else if (pads < padLen) {
            return padStr.substring(0, pads).concat(str);
        } else {
            char[] padding = new char[pads];
            char[] padChars = padStr.toCharArray();
            for (int i = 0; i < pads; i++) {
                padding[i] = padChars[i % padLen];
            }
            return new String(padding).concat(str);
        }
    }


Generated by  Doxygen 1.6.0   Back to index