StringUtils的leftpad和righted方法源码分析

依赖:

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

leftPad:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* StringUtils.leftPad(null, *)   = null
* StringUtils.leftPad("", 3)     = "   "
* StringUtils.leftPad("bat", 3)  = "bat"
* StringUtils.leftPad("bat", 5)  = "  bat"
* StringUtils.leftPad("bat", 1)  = "bat"
* StringUtils.leftPad("bat", -1) = "bat"
*/
public static String leftPad(String str, int size) {
    return leftPad(str, size, ' ');
}
     
/**
* StringUtils.leftPad(null, *, *)     = null
* StringUtils.leftPad("", 3, 'z')     = "zzz"
* StringUtils.leftPad("bat", 3, 'z')  = "bat"
* StringUtils.leftPad("bat", 5, 'z')  = "zzbat"
* StringUtils.leftPad("bat", 1, 'z')  = "bat"
* StringUtils.leftPad("bat", -1, 'z') = "bat"
*/
public static String leftPad(String str, int size, char padChar) {
    if (str == null) {
        return null;
    } else {
        int pads = size - str.length();
        if (pads <= 0) {
            return str;
        } else {
            return pads > 8192 ? leftPad(str, size, String.valueOf(padChar)) : repeat(padChar, pads).concat(str);
        }
    }
}

/**
* 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"
*/
public static String leftPad(String str, int size, String padStr) {
    if (str == null) {
        return null;
    } else {
        if (isEmpty(padStr)) {
            padStr = " ";
        }

        int padLen = padStr.length();
        int strLen = str.length();
        int pads = size - strLen;
        if (pads <= 0) {
            return str;
        } else if (padLen == 1 && pads <= 8192) {
            return leftPad(str, size, padStr.charAt(0));
        } else 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);
        }
    }
}

rightPad:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
public static String rightPad(String str, int size) {
    return rightPad(str, size, ' ');
}

public static String rightPad(String str, int size, char padChar) {
    if (str == null) {
        return null;
    } else {
        int pads = size - str.length();
        if (pads <= 0) {
            return str;
        } else {
            return pads > 8192 ? rightPad(str, size, String.valueOf(padChar)) : str.concat(repeat(padChar, pads));
        }
    }
}

public static String rightPad(String str, int size, String padStr) {
    if (str == null) {
        return null;
    } else {
        if (isEmpty(padStr)) {
            padStr = " ";
        }

        int padLen = padStr.length();
        int strLen = str.length();
        int pads = size - strLen;
        if (pads <= 0) {
            return str;
        } else if (padLen == 1 && pads <= 8192) {
            return rightPad(str, size, padStr.charAt(0));
        } else 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));
        }
    }
}