public static String trimWhitespace(String str){
  if (!hasLength(str)) {
    return str;
  }
  StringBuilder sb=new StringBuilder(str);
  while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) {
    sb.deleteCharAt(0);
  }
  while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) {
    sb.deleteCharAt(sb.length() - 1);
  }
  return sb.toString();
}

public static String trimWhitespace ( String str ) { if ( ! hasLength ( str ) ) { return str ; } StringBuilder sb = new StringBuilder ( str ) ; while ( sb . length ( ) > 0 && Character . isWhitespace ( sb . charAt ( 0 ) ) ) { sb . deleteCharAt ( 0 ) ; } while ( sb . length ( ) > 0 && Character . isWhitespace ( sb . charAt ( sb . length ( ) - 1 ) ) ) { sb . deleteCharAt ( sb . length ( ) - 1 ) ; } return sb . toString ( ) ; }
trim leading and trailing whitespace from the given string .
