Java String align right with filling character
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { CharSequence cs = "demo2s.com"; int width = 30; char c = '.'; System.out.println(alignRight(cs, width, c)); }// ww w. ja v a 2s . c om public static String alignRight(CharSequence cs, int width, char c) { if (cs == null) return null; int len = cs.length(); if (len >= width) return cs.toString(); return dup(c, width - len) + cs; } 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 ((cs == null) || (cs.length() == 0)); } }