code: public static String collapseWhitespace(String str) {
  
  	if (isEmpty(str)) return str;
  
  	StringBuilder sb = new StringBuilder(str.length());
  	boolean wasLastWs = false;
  	char[] carr = str.trim().toCharArray();
  	for (int i = 0; i < carr.length; i++) {
  	    if (isWhiteSpace(carr[i])) {
  		if (wasLastWs) continue;
  
  		sb.append(' ');
  		wasLastWs = true;
  	    }
  	    else {
  		sb.append(carr[i]);
  		wasLastWs = false;
  	    }
  	}
  
  	return sb.toString();
      }
docs: collapses multiple whitespace characters into a single space. the whitespace returned is always a
  standard chr(32) .
  
  @param str
  @return