public static String escape ( String string ) { char c ; String s = string . trim ( ) ; int length = s . length ( ) ; StringBuilder sb = new StringBuilder ( 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 .