After Apurv Answer, I have analyzed bytes array of strings.
String: 32, 32, 32, 32, 52, 45, 83, 112, 97, 99, 101, 115, 32, 32, 32, 32, 44, 34, 68, 111, 117, 98, 108, 101, 32, 81, 117, 111, 116, 101, 34, 44, 32, 39, 83, 105, 110, 103, 108, 101, 32, 81, 117, 111, 116, 101, 39, 44, 32, 92, 66, 97, 99, 107, 45, 83, 108, 97, 115, 104, 92, 44, 32, 47, 70, 111, 114, 119, 97, 114, 100, 32, 83, 108, 97, 115, 104, 47, 32
unescaped : 32, 32, 32, 32, 52, 45, 83, 112, 97, 99, 101, 115, 32, 32, 32, 32, 44, 34, 68, 111, 117, 98, 108, 101, 32, 81, 117, 111, 116, 101, 34, 44, 32, 39, 83, 105, 110, 103, 108, 101, 32, 81, 117, 111, 116, 101, 39, 44, 32, 92, 66, 97, 99, 107, 45, 83, 108, 97, 115, 104, 92, 44, 32, 47, 70, 111, 114, 119, 97, 114, 100, 32, 83, 108, 97, 115, 104, 47, 32
myUnescaped: -96, -96, -96, -96, 52, 45, 83, 112, 97, 99, 101, 115, -96, -96, -96, -96, 44, 34, 68, 111, 117, 98, 108, 101, -96, 81, 117, 111, 116, 101, 34, 44, -96, 39, 83, 105, 110, 103, 108, 101, -96, 81, 117, 111, 116, 101, 39, 44, -96, 92, 66, 97, 99, 107, 45, 83, 108, 97, 115, 104, 92, 44, -96, 47, 70, 111, 114, 119, 97, 114, 100, -96, 83, 108, 97, 115, 104, 47, -96
I seems in myUnescaped
, spaces has converted to ascii -96
instead of 32
.
So I wrote an unescapeHtml
method as below. This method first replaces  
with a space and then use StringEscapeUtils
to unescape html.
public static String unescapeHtml(String s) {
String unescaped = "";
if(null != s) {
unescaped = s.replaceAll(" ", " ");
unescaped = StringEscapeUtils.unescapeHtml(unescaped);
}
return unescaped;
}
Then I got myUnescaped
using following code.
String myUnescaped = unescapeHtml(myEscaped);
This gave me myUnescaped
string equal to string
and unescaped
.
ALTERNATIVELY I replaced
with  
. This does not require me to write unescapeHtml
mehod. Updated escapeHtml
method's code is given below.
public static String escapeHtml(String s) {
String escaped = "";
if(null != s) {
escaped = StringEscapeUtils.escapeHtml(s);
escaped = escaped.replaceAll(" "," "); //updated line
escaped = escaped.replaceAll("'","'");
escaped = escaped.replaceAll("\\\\","\");
escaped = escaped.replaceAll("/","/");
}
return escaped;
}