DroidStringUtils { public static String abbreviate(String str, int minLeft, int maxLength) { if (str.length() <= maxLength) { return str; } int left = maxLength - ELIPSES.length(); left = (left >= 0) ? left : 0; if (left > minLeft && minLeft >= 0) { left = minLeft; } int length = str.length(); if (left > length) { left = length; } StringBuilder sb = new StringBuilder(); sb.append(str.substring(0, left)); sb.append(ELIPSES); int removeCount = length - maxLength + sb.length(); if (removeCount > 0 && removeCount < length) { sb.append(str.substring(removeCount, length)); } return sb.toString(); } private  DroidStringUtils(); static String abbreviate(String str, int minLeft, int maxLength); static String abbreviate(String str, JComponent component);  }
@Test public void testAbbreviate() { String s = "All work and no play makes Jack a dull boy"; assertEquals("All w...dull boy", DroidStringUtils.abbreviate(s, 5, 16)); assertEquals("All w... dull boy", DroidStringUtils.abbreviate(s, 5, 17)); assertEquals("All w...a dull boy", DroidStringUtils.abbreviate(s, 5, 18)); assertEquals("All w... makes Jack a dull boy", DroidStringUtils.abbreviate(s, 5, 30)); assertEquals("A...", DroidStringUtils.abbreviate(s, 5, 4)); assertEquals("All work and no play makes Jack a dull boy", DroidStringUtils.abbreviate(s, 5, s.length())); assertEquals("All work and no play makes Jack a dull boy", DroidStringUtils.abbreviate(s, 5, 60)); assertEquals("All work and no p...", DroidStringUtils.abbreviate(s, 70, 20)); }