public static TrimmedInput trim(final String input,final boolean retainLineSeparator){
  if (input != null) {
    String inputCopy=input;
    StringBuffer output=new StringBuffer();
    inputCopy=StringUtils.stripEnd(inputCopy,null);
    while (Pattern.matches("^(.*)(\\s-+)$",inputCopy)) {
      inputCopy=StringUtils.removeEnd(inputCopy,SyntaxConstants.SHORT_OPTION_SPECIFIER);
      inputCopy=StringUtils.stripEnd(inputCopy,null);
    }
    int noOfSpacesRemoved=0;
{
      int length=inputCopy.length();
      inputCopy=inputCopy.trim();
      noOfSpacesRemoved+=length - inputCopy.length();
    }
    StringBuffer buffer=new StringBuffer();
    boolean startWhiteSpace=false;
    for (int i=0; i < inputCopy.length(); i++) {
      char ch=inputCopy.charAt(i);
      buffer.append(ch);
      if (PreprocessorUtils.isWhitespace(ch)) {
        if (PreprocessorUtils.isSyntaxValid(buffer.toString())) {
          if (startWhiteSpace) {
            noOfSpacesRemoved++;
          }
 else {
            startWhiteSpace=true;
            if (ch == '\n') {
              if (retainLineSeparator) {
                output.append("\n");
              }
            }
 else {
              output.append(" ");
            }
          }
          buffer.delete(0,buffer.length());
        }
 else {
          output.append(ch);
        }
      }
 else {
        startWhiteSpace=false;
        output.append(ch);
      }
    }
    return new TrimmedInput(output.toString(),noOfSpacesRemoved);
  }
 else {
    return null;
  }
}

public static TrimmedInput trim ( final String input , final boolean retainLineSeparator ) { if ( input != null ) { String inputCopy = input ; StringBuffer output = new StringBuffer ( ) ; inputCopy = StringUtils . stripEnd ( inputCopy , null ) ; while ( Pattern . matches ( "^(.*)(\\s-+)$" , inputCopy ) ) { inputCopy = StringUtils . removeEnd ( inputCopy , SyntaxConstants . SHORT_OPTION_SPECIFIER ) ; inputCopy = StringUtils . stripEnd ( inputCopy , null ) ; } int noOfSpacesRemoved = 0 ; { int length = inputCopy . length ( ) ; inputCopy = inputCopy . trim ( ) ; noOfSpacesRemoved += length - inputCopy . length ( ) ; } StringBuffer buffer = new StringBuffer ( ) ; boolean startWhiteSpace = false ; for ( int i = 0 ; i < inputCopy . length ( ) ; i ++ ) { char ch = inputCopy . charAt ( i ) ; buffer . append ( ch ) ; if ( PreprocessorUtils . isWhitespace ( ch ) ) { if ( PreprocessorUtils . isSyntaxValid ( buffer . toString ( ) ) ) { if ( startWhiteSpace ) { noOfSpacesRemoved ++ ; } else { startWhiteSpace = true ; if ( ch == '\n' ) { if ( retainLineSeparator ) { output . append ( "\n" ) ; } } else { output . append ( " " ) ; } } buffer . delete ( 0 , buffer . length ( ) ) ; } else { output . append ( ch ) ; } } else { startWhiteSpace = false ; output . append ( ch ) ; } } return new TrimmedInput ( output . toString ( ) , noOfSpacesRemoved ) ; } else { return null ; } }
this function will trim the given input string . it will not only remove the spaces and tabs at the end but also compress multiple spaces and tabs to a single space
