/* This is taken from a static library of useful functions I made. * * @author Russell Young tetrii@young-0.com * @version 1.0 */ /* Feel free to use, distribute, or change, with the only restriction that you * should not remove my name from the original code, and you should document * your changes so there is no confusion about whose bugs they are. */ public class my { /** pads an integer on the left with 0's. * @param num the int to pad * @param length the int length to make it * @return the String padded integer */ public static String pad(int num, int length) { String s = "" + num; return(pad(s, length, '0', true)); } /** pads a String on the left with blanks. * @param s the String to pad * @param length the int length to make it * @return the String padded */ public static String pad(String s, int length) { return(pad(s, length, ' ', true)); } /** pads a String on the left. * @param s the String to pad * @param length the int length to make it * @param c the char to use for padding * @return the String padded */ public static String pad(String s, int length, char c) { return(pad(s, length, c, true));} /** pads a String with blanks * @param s the String to pad * @param length the int length to make it * @param left a boolean: true for pad left, false for pad right * @return the String padded */ public static String pad(String s, int length, boolean left) { return(pad(s, length, ' ', left));} /** returns a padded string. * @param s a String to pad * @param length the int length to pad to * @param c the char to use * @param left boolean, pad on the left or the right * @return the String padded */ public static String pad(String s, int length, char c, boolean left) { if (s == null) s = ""; int i = s.length() - length; if (i >= 0) return(s.substring(i)); return(left? getPad(c, -i) + s : s + getPad(c, -i)); } /** internal function to build a String of arbitrary length of a given * character, for use by above padding functions. * * @param ch the char to use * @param length the int length to pad to * @return a String of length chars */ private static String getPad(char ch, int length) { StringBuffer sb = new StringBuffer(length); while (length-- > 0) sb.append(ch); return(sb.toString()); } }