最近在写一个查询操作时,忽略了对于特殊字符的处理,比如’%‘和’_’,它们在数据库中作为通配符,因此,不加以处理会导致数据读取产生一定的问题,那么只需要对特殊的字符进行转义,如下:
/**
* @Param [String str]
* @Return String
* @Description 对特殊字符进行过滤
* @Author zbw
* @Time 2020/6/8 17:54
*/
public static String escapeStr(String str) {
String temp = "";
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '%' || str.charAt(i) == '_') {
temp += "\\" + str.charAt(i);
} else {
temp += str.charAt(i);
}
}
return temp;
}