Java String Pad Right rightPad(String str, int size)

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

Description

right Pad

License

Open Source License

Declaration

public static String rightPad(String str, int size) 

Method Source Code

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

public class Main {
    public static String rightPad(String str, int size) {
        return rightPad(str, size, ' ');
    }//  w  w w  .  j  av a 2s  .  c  o m

    public static String rightPad(String str, int size, char padChar) {
        if (str == null) {
            return null;
        } else {
            int pads = size - str.length();
            return pads <= 0 ? str
                    : (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));
            }
        }
    }

    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. rightPad(String str, int num, String padStr)
  2. rightPad(String str, int size)
  3. rightPad(String str, int size)
  4. rightPad(String str, int size)
  5. rightPad(String str, int size)
  6. rightPad(String str, int size, char padChar)
  7. rightPad(String str, int size, char padChar)
  8. rightPad(String str, int size, char padChar)
  9. rightPad(String str, int size, char padChar)