public static String escape(String string){
  char c;
  String s=string.trim();
  StringBuffer sb=new StringBuffer();
  int length=s.length();
  for (int i=0; i < length; i+=1) {
    c=s.charAt(i);
    if (c < ' ' || c == '+' || c == '%' || c == '=' || c == ';') {
      sb.append('%');
      sb.append(Character.forDigit((char)((c >>> 4) & 0x0f),16));
      sb.append(Character.forDigit((char)(c & 0x0f),16));
    }
 else {
      sb.append(c);
    }
  }
  return sb.toString();
}

public static String escape ( String string ) { char c ; String s = string . trim ( ) ; StringBuffer sb = new StringBuffer ( ) ; int length = s . length ( ) ; for ( int i = 0 ; i < length ; i += 1 ) { c = s . charAt ( i ) ; if ( c < ' ' || c == '+' || c == '%' || c == '=' || c == ';' ) { sb . append ( '%' ) ; sb . append ( Character . forDigit ( ( char ) ( ( c > > > 4 ) & 0x0f ) , 16 ) ) ; sb . append ( Character . forDigit ( ( char ) ( c & 0x0f ) , 16 ) ) ; } else { sb . append ( c ) ; } } return sb . toString ( ) ; }
produce a copy of a string in which the characters ' + ' , ' % ' , ' = ' , ' ; ' and control characters are replaced with " % hh " . this is a gentle form of url encoding , attempting to cause as little distortion to the string as possible . the characters ' = ' and ' ; ' are meta characters in cookies . by convention , they are escaped using the url - encoding . this is only a convention , not a standard . often , cookies are expected to have encoded values . we encode ' = ' and ' ; ' because we must . we encode ' % ' and ' + ' because they are meta characters in url encoding .
