Java String Align Right alignRight(CharSequence cs, int width, char c)

Here you can find the source of alignRight(CharSequence cs, int width, char c)

Description

align Right

License

Apache License

Declaration

public static String alignRight(CharSequence cs, int width, char c) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {

    public static String alignRight(CharSequence cs, int width, char c) {
        if (null == cs)
            return null;
        int len = cs.length();
        if (len >= width)
            return cs.toString();
        return new StringBuilder().append(dup(c, width - len)).append(cs).toString();
    }// w w w . j  a v a 2 s.c o  m

    public static String dup(CharSequence cs, int num) {
        if (isEmpty(cs) || num <= 0)
            return "";
        StringBuilder sb = new StringBuilder(cs.length() * num);
        for (int i = 0; i < num; i++)
            sb.append(cs);
        return sb.toString();
    }

    public static String dup(char c, int num) {
        if (c == 0 || num < 1)
            return "";
        StringBuilder sb = new StringBuilder(num);
        for (int i = 0; i < num; i++)
            sb.append(c);
        return sb.toString();
    }

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

Related

  1. alignRight(final long number, final int length)
  2. alignRight(final Object str, final int size)
  3. alignRight(String data)
  4. alignRight(String str, int length, boolean isEllipsis)