StringUtil { public static String normaliseWhitespace(String string) { StringBuilder sb = new StringBuilder(string.length()); appendNormalisedWhitespace(sb, string, false); return sb.toString(); }  static String join(Collection strings, String sep); static String join(Iterator strings, String sep); static String padding(int width); static boolean isBlank(String string); static boolean isNumeric(String string); static boolean isWhitespace(int c); static String normaliseWhitespace(String string); static void appendNormalisedWhitespace(StringBuilder accum, String string, boolean stripLeading); static boolean in(String needle, String... haystack); static boolean inSorted(String needle, String[] haystack); static URL resolve(URL base, String relUrl); static String resolve(final String baseUrl, final String relUrl);  }
@Test public void normaliseWhiteSpace() { Assert.assertEquals(" ", StringUtil.normaliseWhitespace(" \r \n \r\n")); Assert.assertEquals(" hello there ", StringUtil.normaliseWhitespace(" hello \r \n there \n")); Assert.assertEquals("hello", StringUtil.normaliseWhitespace("hello")); Assert.assertEquals("hello there", StringUtil.normaliseWhitespace("hello\nthere")); }
@Test public void normaliseWhiteSpaceHandlesHighSurrogates() { String test71540chars = "\ud869\udeb2\u304b\u309a 1"; String test71540charsExpectedSingleWhitespace = "\ud869\udeb2\u304b\u309a 1"; Assert.assertEquals(test71540charsExpectedSingleWhitespace, StringUtil.normaliseWhitespace(test71540chars)); String extractedText = Jsoup.parse(test71540chars).text(); assertEquals(test71540charsExpectedSingleWhitespace, extractedText); }