@Test public void testTruncateMiddle() { String testString00 = "In Deutschland gibt es rund 120.000 Verm\u00F6gensmillion\u00E4re."; String testString01 = "Dies ist ein Blindtext. Blindtexte sind zumeist " + "weder informativ noch interessant, sondern ausgesprochen " + "langweilig. So auch dieser."; String testString02 = "Ich bin nur ein kleiner Blindtext. Wenn ich gross " + "bin, will ich Ulysses von James Joyce werden. Aber jetzt lohnt es " + "sich noch nicht, mich weiterzulesen. Denn vorerst bin ich nur ein " + "kleiner Blindtext."; String result00 = StringHelper.truncateMiddle(testString00, 56, null); Assert.assertEquals(testString00, result00); Assert.assertFalse(StringUtils.contains(result00, REPLACEMENT_2)); String result01 = StringHelper.truncateMiddle(testString00, 20, null); Assert.assertEquals(result01.length(), 20); Assert.assertTrue(StringUtils.contains(result01, REPLACEMENT_2)); String result02 = StringHelper.truncateMiddle(testString00, 15, null); Assert.assertEquals(result02.length(), 15); Assert.assertTrue(StringUtils.contains(result02, REPLACEMENT_2)); String result03 = StringHelper.truncateMiddle(testString01, 45, REPLACEMENT_1); Assert.assertEquals(result03.length(), 45); Assert.assertTrue(StringUtils.contains(result03, REPLACEMENT_1)); String result04 = StringHelper.truncateMiddle(testString02, 156, REPLACEMENT_3); Assert.assertEquals(result04.length(), 156); Assert.assertTrue(StringUtils.contains(result04, REPLACEMENT_3)); }
public static String truncateMiddle(String string, int maxLength, String ellipses) { int strlen = string.length(); if (strlen <= maxLength) { return string; } if (StringUtils.isBlank(ellipses)) { ellipses = "..."; } int lengthToKeep = maxLength - ellipses.length(); int start = 0; int end = 0; if ((lengthToKeep % 2) == 0) { start = lengthToKeep / 2; end = start; } else { start = lengthToKeep / 2; end = start + 1; } return StringUtils.substring(string, 0, start) + ellipses + StringUtils.substring(string, -1 * end); }
StringHelper { public static String truncateMiddle(String string, int maxLength, String ellipses) { int strlen = string.length(); if (strlen <= maxLength) { return string; } if (StringUtils.isBlank(ellipses)) { ellipses = "..."; } int lengthToKeep = maxLength - ellipses.length(); int start = 0; int end = 0; if ((lengthToKeep % 2) == 0) { start = lengthToKeep / 2; end = start; } else { start = lengthToKeep / 2; end = start + 1; } return StringUtils.substring(string, 0, start) + ellipses + StringUtils.substring(string, -1 * end); } }
StringHelper { public static String truncateMiddle(String string, int maxLength, String ellipses) { int strlen = string.length(); if (strlen <= maxLength) { return string; } if (StringUtils.isBlank(ellipses)) { ellipses = "..."; } int lengthToKeep = maxLength - ellipses.length(); int start = 0; int end = 0; if ((lengthToKeep % 2) == 0) { start = lengthToKeep / 2; end = start; } else { start = lengthToKeep / 2; end = start + 1; } return StringUtils.substring(string, 0, start) + ellipses + StringUtils.substring(string, -1 * end); } private  StringHelper(); }
StringHelper { public static String truncateMiddle(String string, int maxLength, String ellipses) { int strlen = string.length(); if (strlen <= maxLength) { return string; } if (StringUtils.isBlank(ellipses)) { ellipses = "..."; } int lengthToKeep = maxLength - ellipses.length(); int start = 0; int end = 0; if ((lengthToKeep % 2) == 0) { start = lengthToKeep / 2; end = start; } else { start = lengthToKeep / 2; end = start + 1; } return StringUtils.substring(string, 0, start) + ellipses + StringUtils.substring(string, -1 * end); } private  StringHelper(); static String cleanString(String string); static List<String> cleanStringList(List<String> list); static String cleanUpString(String str, String cleanUpChar, String replaceChar); static Long getStringAsLong(String value, Long fallback); static Long[] getStringAsLongArray(String value); static Long[] getStringAsLongArray(String[] values); static List<Long> getStringAsLongList(String value); static List<Long> getStringAsLongList(String value, boolean ignoreValuesEqualLessZero); static List<Long> getStringAsLongList(String value, String separator); static List<Long> getStringAsLongList(String value, String separator,
            boolean ignoreValuesEqualLessZero); static List<Long> getStringAsLongList(String[] values); static Map<String, String> getStringAsMap(String mapping); static String replaceCharacter(String source, char charToReplace, String replacement); static String toString(Collection<?> coll); static String toString(Map<String, String> mapping); static String toString(Object[] array, String separator); static String truncateMiddle(String string, int maxLength, String ellipses); }
StringHelper { public static String truncateMiddle(String string, int maxLength, String ellipses) { int strlen = string.length(); if (strlen <= maxLength) { return string; } if (StringUtils.isBlank(ellipses)) { ellipses = "..."; } int lengthToKeep = maxLength - ellipses.length(); int start = 0; int end = 0; if ((lengthToKeep % 2) == 0) { start = lengthToKeep / 2; end = start; } else { start = lengthToKeep / 2; end = start + 1; } return StringUtils.substring(string, 0, start) + ellipses + StringUtils.substring(string, -1 * end); } private  StringHelper(); static String cleanString(String string); static List<String> cleanStringList(List<String> list); static String cleanUpString(String str, String cleanUpChar, String replaceChar); static Long getStringAsLong(String value, Long fallback); static Long[] getStringAsLongArray(String value); static Long[] getStringAsLongArray(String[] values); static List<Long> getStringAsLongList(String value); static List<Long> getStringAsLongList(String value, boolean ignoreValuesEqualLessZero); static List<Long> getStringAsLongList(String value, String separator); static List<Long> getStringAsLongList(String value, String separator,
            boolean ignoreValuesEqualLessZero); static List<Long> getStringAsLongList(String[] values); static Map<String, String> getStringAsMap(String mapping); static String replaceCharacter(String source, char charToReplace, String replacement); static String toString(Collection<?> coll); static String toString(Map<String, String> mapping); static String toString(Object[] array, String separator); static String truncateMiddle(String string, int maxLength, String ellipses);  }