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