Java特殊字元轉義(、"、/、&)
阿新 • • 發佈:2019-02-08
public static String filter(String value)
{
if(value == null){
return null;
}else{
char content[] = new char[value.length()];
value.getChars(0, value.length(), content, 0);
StringBuffer result = new StringBuffer(content.length + 50);
for(int i = 0; i < content.length; i++)
switch(content[i])
{
case 34: // '"'
result.append(""");
break;
case 38: // '&'
result.append("&");
break;
case 39: // '\''
result.append("'" );
break;
case 60: // '<'
result.append("<");
break;
case 62: // '>'
result.append(">");
break;
default:
result.append(content[i]);
break ;
}
}
return result.toString();
}