慕丝7291255
好短的方法:public static String escapeHTML(String s) {
StringBuilder out = new StringBuilder(Math.max(16, s.length()));
for (int i = 0; i
char c = s.charAt(i);
if (c > 127 || c == '"' || c == '' || c == '&') {
out.append("");
out.append((int) c);
out.append(';');
} else {
out.append(c);
}
}
return out.toString();}基于https://stackoverflow.com/a/8838023/1199155(放大器不见了)。在if子句中签入的四个字符是128以下的唯一字符。http://www.w3.org/TR/html4/sgml/entities.html