自己對html字串轉義

不淨之心發表於2013-04-04
[url]http://lxs647.iteye.com/blog/800722[/url]

public static String htmlEncode(String source) {
if (source == null) {
return "";
}
String html = "";
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < source.length(); i++) {
char c = source.charAt(i);
switch (c) {
case '<':
buffer.append("<");
break;
case '>':
buffer.append(">");
break;
case '&':
buffer.append("&");
break;
case '"':
buffer.append(""");
break;
case ' ':
buffer.append(" ");
break;
case 9:
buffer.append("     ");
break;
case 10:
case 13:
break;
default:
buffer.append(c);
}
}
html = buffer.toString();
return html;
}

相關文章