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

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

Right pad a String with a specified String.

The String is padded to the size of size.

 StringUtils.rightPad(null, *, *)      = null
 StringUtils.rightPad("", 3, "z")      = "zzz"
 StringUtils.rightPad("bat", 3, "yz")  = "bat"
 StringUtils.rightPad("bat", 5, "yz")  = "batyz"
 StringUtils.rightPad("bat", 8, "yz")  = "batyzyzy"
 StringUtils.rightPad("bat", 1, "yz")  = "bat"
 StringUtils.rightPad("bat", -1, "yz") = "bat"
 StringUtils.rightPad("bat", 5, null)  = "bat  "
 StringUtils.rightPad("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:
right padded String or original String if no padding is necessary, null if null String input

Definition at line 3156 of file StringUtils.java.

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

                                                                       {
        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 rightPad(str, size, padStr.charAt(0));
        }
        
        if (pads == padLen) {
            return str.concat(padStr);
        } else if (pads < padLen) {
            return str.concat(padStr.substring(0, pads));
        } else {
            char[] padding = new char[pads];
            char[] padChars = padStr.toCharArray();
            for (int i = 0; i < pads; i++) {
                padding[i] = padChars[i % padLen];
            }
            return str.concat(new String(padding));
        }
    }


Generated by  Doxygen 1.6.0   Back to index