StringUtil { public static String trim(final String str, final char ch) { if (isEmpty(str)) { return null; } final char[] chars = str.toCharArray(); int i = 0, j = chars.length - 1; for (; i < chars.length && chars[i] == ch; i++) { } for (; j > 0 && chars[j] == ch; j--) { } return new String(chars, i, j - i + 1); }  static boolean isEmpty(String str); static boolean isNotEmpty(String str); static void setIfPresent(String value, Consumer<String> setter); static String join(final char delimiter, final String... strings); static boolean substringMatch(CharSequence str, int index, CharSequence substring); static String cut(String str, int threshold); static String trim(final String str, final char ch);  }
@Test public void testTrim() { Assert.assertEquals(StringUtil.trim("aaabcdefaaa", 'a'), "bcdef"); Assert.assertEquals(StringUtil.trim("bcdef", 'a'), "bcdef"); Assert.assertEquals(StringUtil.trim("abcdef", 'a'), "bcdef"); Assert.assertEquals(StringUtil.trim("abcdef", 'f'), "abcde"); }