public static String suppressWhiteSpace(String str){
  int len=str.length();
  StringBuilder sb=new StringBuilder(len);
  char c;
  char buffer=0;
  for (int i=0; i < len; i++) {
    c=str.charAt(i);
    if (c == '\n' || c == '\r')     buffer='\n';
 else     if (isWhiteSpace(c)) {
      if (buffer == 0)       buffer=c;
    }
 else {
      if (buffer != 0) {
        sb.append(buffer);
        buffer=0;
      }
      sb.append(c);
    }
  }
  if (buffer != 0)   sb.append(buffer);
  return sb.toString();
}

public static String suppressWhiteSpace ( String str ) { int len = str . length ( ) ; StringBuilder sb = new StringBuilder ( len ) ; char c ; char buffer = 0 ; for ( int i = 0 ; i < len ; i ++ ) { c = str . charAt ( i ) ; if ( c == '\n' || c == '\r' ) buffer = '\n' ; else if ( isWhiteSpace ( c ) ) { if ( buffer == 0 ) buffer = c ; } else { if ( buffer != 0 ) { sb . append ( buffer ) ; buffer = 0 ; } sb . append ( c ) ; } } if ( buffer != 0 ) sb . append ( buffer ) ; return sb . toString ( ) ; }
remove all white spaces followd by whitespaces
