static public String escapeTags(final String input){
  if (input == null || input.length() == 0 || (input.indexOf('"') == -1 && input.indexOf('&') == -1 && input.indexOf('<') == -1 && input.indexOf('>') == -1)) {
    return input;
  }
  StringBuilder buf=new StringBuilder(input.length() + 6);
  char ch;
  int len=input.length();
  for (int i=0; i < len; i++) {
    ch=input.charAt(i);
    if (ch > '>') {
      buf.append(ch);
    }
 else     if (ch == '<') {
      buf.append("&lt;");
    }
 else     if (ch == '>') {
      buf.append("&gt;");
    }
 else     if (ch == '&') {
      buf.append("&amp;");
    }
 else     if (ch == '"') {
      buf.append("&quot;");
    }
 else {
      buf.append(ch);
    }
  }
  return buf.toString();
}

static public String escapeTags ( final String input ) { if ( input == null || input . length ( ) == 0 || ( input . indexOf ( '"' ) == - 1 && input . indexOf ( '&' ) == - 1 && input . indexOf ( '<' ) == - 1 && input . indexOf ( '>' ) == - 1 ) ) { return input ; } StringBuilder buf = new StringBuilder ( input . length ( ) + 6 ) ; char ch ; int len = input . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { ch = input . charAt ( i ) ; if ( ch > '>' ) { buf . append ( ch ) ; } else if ( ch == '<' ) { buf . append ( "&lt;" ) ; } else if ( ch == '>' ) { buf . append ( "&gt;" ) ; } else if ( ch == '&' ) { buf . append ( "&amp;" ) ; } else if ( ch == '"' ) { buf . append ( "&quot;" ) ; } else { buf . append ( ch ) ; } } return buf . toString ( ) ; }
this method takes a string which may contain html tags ( ie , & lt ; b & gt ; , & lt ; table & gt ; , etc ) and replaces any ' < ' , ' > ' , ' & ' or ' " ' characters with respective predefined entity references .
