@Test public void testPad() { assertThat(Format.pad("123", 2, true)).isEqualTo("123"); assertThat(Format.pad("123", 2, false)).isEqualTo("123"); assertThat(Format.pad("123", 3, true)).isEqualTo("123"); assertThat(Format.pad("123", 3, false)).isEqualTo("123"); assertThat(Format.pad("123", 4, true)).isEqualTo(" 123"); assertThat(Format.pad("123", 4, false)).isEqualTo("123 "); }
public static String pad(String s, int width, boolean rightJustify) { if (s.length() >= width) { return s; } StringBuilder sbuff = new StringBuilder(width); int need = width - s.length(); sbuff.setLength(need); for (int i = 0; i < need; i++) { sbuff.setCharAt(i, ' '); } if (rightJustify) { sbuff.append(s); } else { sbuff.insert(0, s); } return sbuff.toString(); }
Format { public static String pad(String s, int width, boolean rightJustify) { if (s.length() >= width) { return s; } StringBuilder sbuff = new StringBuilder(width); int need = width - s.length(); sbuff.setLength(need); for (int i = 0; i < need; i++) { sbuff.setCharAt(i, ' '); } if (rightJustify) { sbuff.append(s); } else { sbuff.insert(0, s); } return sbuff.toString(); } }
Format { public static String pad(String s, int width, boolean rightJustify) { if (s.length() >= width) { return s; } StringBuilder sbuff = new StringBuilder(width); int need = width - s.length(); sbuff.setLength(need); for (int i = 0; i < need; i++) { sbuff.setCharAt(i, ' '); } if (rightJustify) { sbuff.append(s); } else { sbuff.insert(0, s); } return sbuff.toString(); }  }
Format { public static String pad(String s, int width, boolean rightJustify) { if (s.length() >= width) { return s; } StringBuilder sbuff = new StringBuilder(width); int need = width - s.length(); sbuff.setLength(need); for (int i = 0; i < need; i++) { sbuff.setCharAt(i, ' '); } if (rightJustify) { sbuff.append(s); } else { sbuff.insert(0, s); } return sbuff.toString(); }  static void tab(StringBuilder sbuff, int tabStop, boolean alwaysOne); static String s(String s, int width); static String pad(String s, int width, boolean rightJustify); static String i(int v, int width); static String l(long v, int width); static String d(double d, int min_sigfig); static String d(double d, int min_sigfig, int width); static String dfrac(double d, int fixed_decimals); static String formatDouble(double d, int min_sigFigs, int fixed_decimals); static String formatByteSize(double size); }
Format { public static String pad(String s, int width, boolean rightJustify) { if (s.length() >= width) { return s; } StringBuilder sbuff = new StringBuilder(width); int need = width - s.length(); sbuff.setLength(need); for (int i = 0; i < need; i++) { sbuff.setCharAt(i, ' '); } if (rightJustify) { sbuff.append(s); } else { sbuff.insert(0, s); } return sbuff.toString(); }  static void tab(StringBuilder sbuff, int tabStop, boolean alwaysOne); static String s(String s, int width); static String pad(String s, int width, boolean rightJustify); static String i(int v, int width); static String l(long v, int width); static String d(double d, int min_sigfig); static String d(double d, int min_sigfig, int width); static String dfrac(double d, int fixed_decimals); static String formatDouble(double d, int min_sigFigs, int fixed_decimals); static String formatByteSize(double size);  }