Java String Align Right alignRight(String str, int size, char padChar)

Here you can find the source of alignRight(String str, int size, char padChar)

Description

align Right

License

Open Source License

Declaration

public static String alignRight(String str, int size, char padChar) 

Method Source Code

//package com.java2s;

public class Main {

    public static String alignRight(String str, int size) {
        return alignRight(str, size, ' ');
    }//ww w.  j  av  a 2s.  c om

    public static String alignRight(String str, int size, char padChar) {
        if (str == null) {
            return null;
        }

        int pads = size - str.length();

        if (pads <= 0) {
            return str;
        }

        return alignRight(str, size, String.valueOf(padChar));
    }

    public static String alignRight(String str, int size, String padStr) {
        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;
        }

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

Related

  1. alignRight(CharSequence cs, int width, char c)
  2. alignRight(final long number, final int length)
  3. alignRight(final Object str, final int size)
  4. alignRight(String data)
  5. alignRight(String str, int length, boolean isEllipsis)
  6. alignRight(String substring, int totalWidth, char fill)
  7. alignRight(String text, int length)
  8. alignRight(String val, char pad, int width)