QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Normalize Whitespace

Replaces consecutive whitespaces with a single space

  • string
  • whitespace
  • normalize

Code

public static String normalizeWhitespace(String text) {
    return text.replaceAll(" {2,}", " ");
}

Usage

System.out.println(normalizeWhitespace("hello   world")); // "hello world"