@Test public void abbreviate() { final int[] maxWidth = new int[]{5, 5, 100}; String str = StringHelper.abbreviate(new String[]{"1", "Hello", "ProjectForge"}, maxWidth, 22, ": "); assertEquals("1: Hello: ProjectForge", str); assertTrue(str.length() <= 22); str = StringHelper.abbreviate(new String[]{"11234567", "Hello, how are you?", "ProjectForge is the world fines Project management app."}, maxWidth, 22, ": "); assertTrue(str.length() == 22); assertEquals("11...: He...: Proje...", str); str = StringHelper.abbreviate(new String[]{null, "1", "ProjectForge is the world fines Project management app."}, maxWidth, 22, ": "); assertTrue(str.length() == 22); assertEquals("1: ProjectForge is ...", str); }
public static String abbreviate(final String[] strings, final int[] maxWidth, final int maxTotalLength, final String separator) { Validate.notNull(strings); Validate.notNull(maxWidth); Validate.isTrue(strings.length == maxWidth.length); int rest = maxTotalLength; final StringBuilder buf = new StringBuilder(); final int separatorLength = separator.length(); boolean output = false; for (int i = 0; i < strings.length; i++) { final String str = strings[i]; if (StringUtils.isBlank(str)) { continue; } if (output) { buf.append(separator); rest -= separatorLength; } else { output = true; } if (rest <= 0) { break; } final int max = Math.min(maxWidth[i], rest); buf.append(StringUtils.abbreviate(str, max)); rest -= Math.min(str.length(), max); } return buf.toString(); }
StringHelper { public static String abbreviate(final String[] strings, final int[] maxWidth, final int maxTotalLength, final String separator) { Validate.notNull(strings); Validate.notNull(maxWidth); Validate.isTrue(strings.length == maxWidth.length); int rest = maxTotalLength; final StringBuilder buf = new StringBuilder(); final int separatorLength = separator.length(); boolean output = false; for (int i = 0; i < strings.length; i++) { final String str = strings[i]; if (StringUtils.isBlank(str)) { continue; } if (output) { buf.append(separator); rest -= separatorLength; } else { output = true; } if (rest <= 0) { break; } final int max = Math.min(maxWidth[i], rest); buf.append(StringUtils.abbreviate(str, max)); rest -= Math.min(str.length(), max); } return buf.toString(); } }
StringHelper { public static String abbreviate(final String[] strings, final int[] maxWidth, final int maxTotalLength, final String separator) { Validate.notNull(strings); Validate.notNull(maxWidth); Validate.isTrue(strings.length == maxWidth.length); int rest = maxTotalLength; final StringBuilder buf = new StringBuilder(); final int separatorLength = separator.length(); boolean output = false; for (int i = 0; i < strings.length; i++) { final String str = strings[i]; if (StringUtils.isBlank(str)) { continue; } if (output) { buf.append(separator); rest -= separatorLength; } else { output = true; } if (rest <= 0) { break; } final int max = Math.min(maxWidth[i], rest); buf.append(StringUtils.abbreviate(str, max)); rest -= Math.min(str.length(), max); } return buf.toString(); }  }
StringHelper { public static String abbreviate(final String[] strings, final int[] maxWidth, final int maxTotalLength, final String separator) { Validate.notNull(strings); Validate.notNull(maxWidth); Validate.isTrue(strings.length == maxWidth.length); int rest = maxTotalLength; final StringBuilder buf = new StringBuilder(); final int separatorLength = separator.length(); boolean output = false; for (int i = 0; i < strings.length; i++) { final String str = strings[i]; if (StringUtils.isBlank(str)) { continue; } if (output) { buf.append(separator); rest -= separatorLength; } else { output = true; } if (rest <= 0) { break; } final int max = Math.min(maxWidth[i], rest); buf.append(StringUtils.abbreviate(str, max)); rest -= Math.min(str.length(), max); } return buf.toString(); }  static boolean append(final StringBuffer buf, final boolean first, final String str, final String delimiter); static boolean append(final StringBuilder sb, final boolean first, final String str, final String delimiter); static int compareTo(final String s1, final String s2); static boolean isIn(final String string, final String... fields); static boolean endsWith(final String str, final String... suffixes); static boolean startsWith(final String str, final String... prefixes); static String listToString(final List<String> list, final String delimiter, final boolean sort); static String colToString(final Collection<String> col, final String delimiter); static String objectColToString(final Collection<?> col, final String delimiter); static String listToString(final String delimiter, final String... strings); static String listToString(final StringBuffer buf, final String delimiter, final String... strings); static String listToString(final String delimiter, final Object... oa); static String doublesToString(final String delimiter, final double... oa); static String listToExpressions(final String delimiter, final String prefix, final String suffix,
      final String... strings); static String[] sortAndUnique(final String[] array); static String dateToSearchString(final Date date); static String format2DigitNumber(final int value); static String format3DigitNumber(final int value); static String removeNonDigits(final String str); static String removeNonDigitsAndNonASCIILetters(final String str); static String abbreviate(final String[] strings, final int[] maxWidth, final int maxTotalLength,
      final String separator); static String removeWhitespaces(final String value); static String getWildcardString(final String... strs); static boolean checkPhoneNumberFormat(final String value); static String hideStringEnding(final String str, final char ch, final int numberOfCharacters); static Integer[] splitToIntegers(final String str, final String delim); static int[] splitToInts(final String str, final String delim); static int[] splitToInts(final String str, final String delim, final boolean ignoreEmptyItems); static String[] splitAndTrim(final String str, final String separatorChars); static boolean isNotBlank(final String... strs); static boolean isBlank(final String... strs); static String asHex(final byte[] buf); static Map<String, String> getKeyValues(final String keyValues, final String delimiter); }
StringHelper { public static String abbreviate(final String[] strings, final int[] maxWidth, final int maxTotalLength, final String separator) { Validate.notNull(strings); Validate.notNull(maxWidth); Validate.isTrue(strings.length == maxWidth.length); int rest = maxTotalLength; final StringBuilder buf = new StringBuilder(); final int separatorLength = separator.length(); boolean output = false; for (int i = 0; i < strings.length; i++) { final String str = strings[i]; if (StringUtils.isBlank(str)) { continue; } if (output) { buf.append(separator); rest -= separatorLength; } else { output = true; } if (rest <= 0) { break; } final int max = Math.min(maxWidth[i], rest); buf.append(StringUtils.abbreviate(str, max)); rest -= Math.min(str.length(), max); } return buf.toString(); }  static boolean append(final StringBuffer buf, final boolean first, final String str, final String delimiter); static boolean append(final StringBuilder sb, final boolean first, final String str, final String delimiter); static int compareTo(final String s1, final String s2); static boolean isIn(final String string, final String... fields); static boolean endsWith(final String str, final String... suffixes); static boolean startsWith(final String str, final String... prefixes); static String listToString(final List<String> list, final String delimiter, final boolean sort); static String colToString(final Collection<String> col, final String delimiter); static String objectColToString(final Collection<?> col, final String delimiter); static String listToString(final String delimiter, final String... strings); static String listToString(final StringBuffer buf, final String delimiter, final String... strings); static String listToString(final String delimiter, final Object... oa); static String doublesToString(final String delimiter, final double... oa); static String listToExpressions(final String delimiter, final String prefix, final String suffix,
      final String... strings); static String[] sortAndUnique(final String[] array); static String dateToSearchString(final Date date); static String format2DigitNumber(final int value); static String format3DigitNumber(final int value); static String removeNonDigits(final String str); static String removeNonDigitsAndNonASCIILetters(final String str); static String abbreviate(final String[] strings, final int[] maxWidth, final int maxTotalLength,
      final String separator); static String removeWhitespaces(final String value); static String getWildcardString(final String... strs); static boolean checkPhoneNumberFormat(final String value); static String hideStringEnding(final String str, final char ch, final int numberOfCharacters); static Integer[] splitToIntegers(final String str, final String delim); static int[] splitToInts(final String str, final String delim); static int[] splitToInts(final String str, final String delim, final boolean ignoreEmptyItems); static String[] splitAndTrim(final String str, final String separatorChars); static boolean isNotBlank(final String... strs); static boolean isBlank(final String... strs); static String asHex(final byte[] buf); static Map<String, String> getKeyValues(final String keyValues, final String delimiter);  }