现在遇到一个问题
String a=" where b = 1 or yylb ='@aa' and c=1 and a='@c' ";
中如何替换含有@符号内容整块为1=1字符串,得到结果是
String a=" where b = 1 or 1=1 and c=1 and 1=1 ";
现在遇到一个问题
String a=" where b = 1 or yylb ='@aa' and c=1 and a='@c' ";
中如何替换含有@符号内容整块为1=1字符串,得到结果是
String a=" where b = 1 or 1=1 and c=1 and 1=1 ";
试试我的这段代码,应该可以解决你的问题,但我觉得应该还有更好的代码
//这是你提供的sql
String str=" where b = 1 or yylb ='@aa' and c=1 and a='@c' ";
//因为后面要用到空格分组获取条件,首先去掉等号左右的空格,这个不会影响sql语句的执行
str=str.replaceAll("\s{0,}=\s{0,}", "=");
StringBuffer bufferSql=new StringBuffer(" ");
//按一个或多个空格分组
String [] strs=str.split("\s{1,}");
for (int i = 0; i < strs.length; i++) {
if(strs[i].indexOf("@")>-1){
bufferSql.append("1=1 ");
}
else{
bufferSql.append(strs[i]+" ");
}
}
System.out.println(bufferSql.toString());
//执行结果" where b=1 or 1=1 and c=1 and 1=1 "