Strings { public static String collapseWhitespace(String x, String whitespaceReplacement) { if (x==null) return null; return replaceAllRegex(x, "\\s+", whitespaceReplacement); }  static boolean isEmpty(CharSequence s); static boolean isBlank(CharSequence s); static boolean isNonEmpty(CharSequence s); static boolean isNonBlank(CharSequence s); static Maybe<T> maybeNonBlank(T s); static void checkNonEmpty(CharSequence s); static void checkNonEmpty(CharSequence s, String message); static String removeFromEnd(String string, String suffix); static String removeAllFromEnd(String string, String... suffixes); static String removeFromStart(String string, String prefix); static String removeAllFromStart(String string, String... prefixes); static String removeAfter(String input, String marker, boolean keepMarker); static String removeBefore(String input, String marker, boolean keepMarker); static String join(Iterable<? extends Object> list, String separator); static String join(Object[] list, String separator); static String lines(String ...lines); static String lines(Iterable<String> lines); static String removeLines(String multiline, Predicate<CharSequence> patternToRemove); @SuppressWarnings("rawtypes") static String replaceAll(String source, Map replacements); static String replaceAll(String source, String pattern, String replacement); static String replaceAllNonRegex(String source, String pattern, String replacement); static String replaceAllRegex(String source, String pattern, String replacement); static String makeValidFilename(String s); static String makeValidJavaName(String s); static String makeValidUniqueJavaName(String s); static String makeRandomId(int l); static String makeZeroPaddedString(int i, int len); static String makePaddedString(String base, int len, String left_pad, String right_pad); static void trimAll(String[] s); static String makeRealString(double x, int maxlen, int prec, int leftPadLen); static String makeRealString(double x, int maxlen, int prec, int leftPadLen, double skipDecimalThreshhold, boolean useEForSmallNumbers); static String makeRealStringNearZero(double x, int maxlen, int prec, int leftPadLen); static String getFirstWord(String s); static String getLastWord(String s); static String getFirstWordAfter(String context, String phrase); static String getRemainderOfLineAfter(String context, String phrase); @Deprecated static String makeTimeString(long utcMillis); static String[] makeArray(String prefix, int count); static String[] combineArrays(String[] ...arrays); static String toInitialCapOnly(String value); static String reverse(String name); static boolean isLowerCase(String s); static String makeRepeated(char c, int length); static String trim(String s); static String trimEnd(String s); static String maxlen(String s, int maxlen); static String maxlenWithEllipsis(String s, int maxlen); static String maxlenWithEllipsis(String s, int maxlen, String ellipsis); static String toString(Object o); static String toStringWithValueForNull(Object o, String valueIfNull); static boolean containsLiteralIgnoreCase(CharSequence input, CharSequence fragment); static boolean containsLiteral(CharSequence input, CharSequence fragment); static boolean containsAny(CharSequence input, CharSequence ...candidates); static String makeSizeString(long sizeInBytes); static String makeISOSizeString(long sizeInBytes); static String makeJavaSizeString(long sizeInBytes); static StringShortener shortener(); static Supplier<String> toStringSupplier(Object src); static FormattedString format(String pattern, Object... args); static String s(int count); static String s(@Nullable Map<?,?> x); static String s(Iterable<?> x); static String s(Iterator<?> x); static String ies(int count); static String ies(@Nullable Map<?,?> x); static String ies(Iterable<?> x); static String ies(Iterator<?> x); static Map<String, String> toStringMap(Map<?,?> map, String valueIfNull); static List<String> toStringList(List<?> list, String valueIfNull); static List<String> toStringList(Object v); static String repeat(String base, int count); static Ordering<String> lengthComparator(); static boolean isMultiLine(String s); static String getFirstLine(String s); static String getFragmentBetween(String input, String prefix, String suffix); static int getWordCount(String phrase, boolean respectQuotes); static char getDecimalSeparator(Locale locale); static char getDefaultDecimalSeparator(); static String collapseWhitespace(String x, String whitespaceReplacement); static String toLowerCase(String value); static String emptyToNull(String var); static String toUniqueString(Object x, int optionalMax); static List<String> parseCsv(String csv, String separatorRegex); static List<String> parseCsv(String csv); static int countOccurrences(String phrase, char target); static final String EMPTY; static final String VALID_NON_ALPHANUM_FILE_CHARS; static final CharMatcher IS_JAVA_IDENTIFIER_PART; }
@Test public void testCollapseWhitespace() { Assert.assertEquals(Strings.collapseWhitespace(" x\n y\n", ""), "xy"); Assert.assertEquals(Strings.collapseWhitespace(" x\n y\n", " "), " x y "); Assert.assertEquals(Strings.collapseWhitespace(" x\n y\n", "\n").trim(), "x\ny"); }