1 4 package com.tc.text; 5 6 public class StringFormatter { 7 8 private final String nl; 9 10 public StringFormatter() { 11 nl = System.getProperty("line.separator"); 12 } 13 14 public String newline() { 15 return nl; 16 } 17 18 public String leftPad(int size, Object s) { 19 return pad(false, size, s); 20 } 21 22 23 public String leftPad(int size, int i) { 24 return leftPad(size, "" + i); 25 } 26 27 public String rightPad(int size, Object s) { 28 return pad(true, size, s); 29 } 30 31 public String rightPad(int size, int i) { 32 return rightPad(size, "" + i); 33 } 34 35 private String pad(boolean right, int size, Object s) { 36 StringBuffer buf = new StringBuffer (); 37 buf.append(s); 38 while (buf.length() < size) { 39 if (right) buf.append(" "); 40 else buf.insert(0, " "); 41 } 42 while (buf.length() > size) { 43 buf.deleteCharAt(buf.length() - 1); 44 if (buf.length() == size) { 45 buf.deleteCharAt(buf.length() - 1).append("~"); 46 } 47 } 48 return buf.toString(); 49 } 50 51 } | Popular Tags |