public static String removeDuplicateWhitespace(String s){
  StringBuilder result=new StringBuilder();
  int length=s.length();
  boolean isPreviousWhiteSpace=false;
  for (int i=0; i < length; i++) {
    char c=s.charAt(i);
    boolean thisCharWhiteSpace=Character.isWhitespace(c);
    if (!(isPreviousWhiteSpace && thisCharWhiteSpace)) {
      result.append(c);
    }
    isPreviousWhiteSpace=thisCharWhiteSpace;
  }
  return result.toString();
}

public static String removeDuplicateWhitespace ( String s ) { StringBuilder result = new StringBuilder ( ) ; int length = s . length ( ) ; boolean isPreviousWhiteSpace = false ; for ( int i = 0 ; i < length ; i ++ ) { char c = s . charAt ( i ) ; boolean thisCharWhiteSpace = Character . isWhitespace ( c ) ; if ( ! ( isPreviousWhiteSpace && thisCharWhiteSpace ) ) { result . append ( c ) ; } isPreviousWhiteSpace = thisCharWhiteSpace ; } return result . toString ( ) ; }
remove all duplicate whitespace characters and line terminators are replaced with a single space .
