StringTrimmer { public String removeTrailingAndLeadingWhitespaces(final String inputStr) { if (StringUtils.isEmpty(inputStr)) { return ""; } int len = inputStr.length(); int startIndex = 0; while ((startIndex < len) && (mustBeRemoved(inputStr.charAt(startIndex)))) { startIndex++; } while ((startIndex < len) && (mustBeRemoved(inputStr.charAt(len - 1)))) { len--; } return ((startIndex > 0) || (len < inputStr.length())) ? inputStr.substring(startIndex, len) : inputStr; }  String removeTrailingAndLeadingWhitespaces(final String inputStr); String removeTrailingAndLeading(String inputStr); String removeTrailingAndLeading(String inputStr, String removeStr); String removeTrailingAndLeading(String inputStr, Character removeCharacter); static final Set<Character> WHITESPACE_CHARS_SUPERIOR_ASCII_SPACE; }
@Test public void testRemoveTrailingAndLeadingWhitespaces() { StringTrimmer stringTrimmer = new StringTrimmer(); String inputData = " " + expected; for (String removechar : WHITESPACE_CHARS) { inputData = inputData + removechar; } assertEquals(expected, stringTrimmer.removeTrailingAndLeadingWhitespaces(inputData)); inputData = ""; for (String removechar : WHITESPACE_CHARS) { inputData = inputData + removechar; } inputData = inputData + expected + " "; assertEquals(expected, stringTrimmer.removeTrailingAndLeadingWhitespaces(inputData)); assertEquals("", stringTrimmer.removeTrailingAndLeadingWhitespaces(" ")); assertEquals("a", stringTrimmer.removeTrailingAndLeadingWhitespaces(" a ")); assertEquals("a", stringTrimmer.removeTrailingAndLeadingWhitespaces("a ")); assertEquals("a", stringTrimmer.removeTrailingAndLeadingWhitespaces(" a")); assertEquals("a", stringTrimmer.removeTrailingAndLeadingWhitespaces(" a ")); }