StringUtils { public static final String normalizeSpace(String str) { int len = str.length(); StringBuilder sb = new StringBuilder(len); boolean space = false; for (int i = 0; i < len; i++) { char ch = str.charAt(i); switch (ch) { case ' ': case '\t': case '\r': case '\n': space = true; break; default: if (space) { sb.append(' '); space = false; } sb.append(ch); } } return sb.toString(); }  static final boolean isEmpty(String str); static final boolean isNotEmpty(String str); static final String join(String[] array, String separator); static final String join(Collection<String> list, String separator); static final String normalizeSpace(String str); static final String trimAll(String str);  }
@Test public void testNormalizeSpace() { Assert.assertEquals(" f o o b a r", StringUtils.normalizeSpace(" f\to\ro\nb a r ")); }