Java String Pad Left leftPad(String str, int size)

Here you can find the source of leftPad(String str, int size)

Description

left Pad

License

Open Source License

Declaration

public static String leftPad(String str, int size) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String leftPad(String str, int size) {
        return leftPad(str, size, ' ');
    }/*from  w w  w . ja v a 2 s  .co m*/

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

    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);
            }
        }
    }

    public static String repeat(String str, int repeat) {
        if (str == null) {
            return null;
        } else if (repeat <= 0) {
            return "";
        } else {
            int inputLength = str.length();
            if (repeat != 1 && inputLength != 0) {
                if (inputLength == 1 && repeat <= 8192) {
                    return repeat(str.charAt(0), repeat);
                } else {
                    int outputLength = inputLength * repeat;
                    switch (inputLength) {
                    case 1:
                        return repeat(str.charAt(0), repeat);
                    case 2:
                        char ch0 = str.charAt(0);
                        char ch1 = str.charAt(1);
                        char[] output2 = new char[outputLength];

                        for (int buf = repeat * 2 - 2; buf >= 0; --buf) {
                            output2[buf] = ch0;
                            output2[buf + 1] = ch1;
                            --buf;
                        }

                        return new String(output2);
                    default:
                        StringBuilder var9 = new StringBuilder(outputLength);

                        for (int i = 0; i < repeat; ++i) {
                            var9.append(str);
                        }

                        return var9.toString();
                    }
                }
            } else {
                return str;
            }
        }
    }

    public static String repeat(char ch, int repeat) {
        if (repeat <= 0) {
            return "";
        } else {
            char[] buf = new char[repeat];

            for (int i = repeat - 1; i >= 0; --i) {
                buf[i] = ch;
            }

            return new String(buf);
        }
    }

    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
}

Related

  1. leftPad(String str, int maxLength, char placeholder)
  2. leftPad(String str, int num, String padStr)
  3. leftPad(String str, int pad)
  4. leftPad(String str, int size)
  5. leftPad(String str, int size)
  6. leftPad(String str, int size)
  7. leftPad(String str, int size)
  8. leftPad(String str, int size)
  9. leftPad(String str, int size)