JAVA对特殊的字符串进行html编码

SourceURL:about:blank

/**
* Created by Administrator on 2016/9/22.
*/
public class HtmlEncode {
public String HtmlEncode(String str) {
int length = str.length();
int newLength = length;
boolean someCharacterEscaped = false;
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
int cint = 0xffff & c;
if (cint < 32)
switch (c) {
case 11:
default:
newLength--;
someCharacterEscaped = true;
break; case '\t':
case '\n':
case '\f':
case '\r':
break;
}
else
switch (c) {
case '"':
newLength += 5;
someCharacterEscaped = true;
break; case '&':
case '\'':
newLength += 4;
someCharacterEscaped = true;
break; case '<':
case '>':
newLength += 3;
someCharacterEscaped = true;
break;
}
}
if (!someCharacterEscaped)
return str; StringBuffer sb = new StringBuffer(newLength);
for (int i = 0; i < length; i++) {
char c = str.charAt(i);
int cint = 0xffff & c;
if (cint < 32)
switch (c) {
case '\t':
case '\n':
case '\f':
case '\r':
sb.append(c);
break;
}
else
switch (c) {
case '"':
sb.append("&quot;");
break; case '\'':
sb.append("&apos;");
break; case '&':
sb.append("&amp;");
break; case '<':
sb.append("&lt;");
break; case '>':
sb.append("&gt;");
break; default:
sb.append(c);
break;
}
}
return sb.toString();
}
}
上一篇:基于STM32F103ZET6 HC_SR04超声波测距模块


下一篇:cloudera-scm-server启动时出现Caused by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not open connection问题解决方法(图文详解)