屏蔽用户手动输入的代码或特殊字符处理
方法一:
public static String processWildCards(String var){
var = StringUtils.replace(var, "'", "''");
var=StringUtils.replace(var, "[", "[[]");
var=StringUtils.replace(var, "_", "[_]");
var=StringUtils.replace(var, "%", "[%]");
return var;
}
方法二:
public static String getTxtWithoutNTSRElement(String str){
String dest = "";
if (str!=null) {
// Pattern p = Pattern.compile("[\\s]|[\t]|[\r]|[\n]|[?]|[^\\p{ASCII}]");
Pattern p = Pattern.compile("[\\s]|[\t]|[\r]|[\n]|[?]|[^\u4E00-\u9FA5\u0000-\u007f]"); //[^\u4E00-\u9FA5[\\p{ASCII}]] //[^\\p{ASCII}]
Matcher m = p.matcher(str);
dest = m.replaceAll("");
}
return dest;
}
方法三:
public static String htmlEncode(String str) {
if (str == null || str.length() == 0) {
return str;
}
StringBuffer sBuf = new StringBuffer(str.length());
for (int i = 0; i < str.length(); i++) {
char content = str.charAt(i);
switch (content) {
case ('<'):
sBuf.append("<");
break;
case ('>'):
sBuf.append(">");
break;
case ('\"'):
sBuf.append(""");
break;
case ('\''):
sBuf.append("'");
break;
case ('('):
sBuf.append("(");
break;
case (')'):
sBuf.append(")");
break;
case ('%'):
sBuf.append("%");
break;
case ('&'):
sBuf.append("&");
break;
case ('+'):
sBuf.append("+");
break;
case (';'):
sBuf.append(";");
break;
default:
sBuf.append(content);
break;
}
}
return sBuf.toString();
}