String sql = "select * from tableA where aaa like 'a' and bbb ='b' and ccc like 'c'";
String newSql = getNewSql(sql);
newSql变为"select * from tableA where aaa like 'a%' and bbb ='b' and ccc like 'c%'"
即有like字段加个%,谁能帮我实现啊,谢谢!在线等!

解决方案 »

  1.   

    想了一个,应该不是很健壮,没想太多public class TestSqlString { public static void main(String[] args) {
    String sql = "select * from tableA where aaa like 'a' and bbb ='b' and ccc like 'c'"; 
    System.out.println(getNewSql(sql));
    }

    public static String getNewSql(String sql) {
    String[] sArray = sql.split(" ");

    for(int i = 0; i < sArray.length; i++) {
    if(sArray[i].equals("like")) {
    sArray[i + 1] = sArray[i + 1].substring(0, sArray[i + 1].lastIndexOf("\'")) + "%'";
    }
    }
    StringBuilder sb = new StringBuilder();
    for(String s : sArray) {
    sb.append(s + " ");
    }
    return sb.toString();
    }
    }
      

  2.   


    String newsql = sql.replaceAll("like '(.*)'", "$1%'");%加字段最后喵~``?
      

  3.   

    测试结果是这样
    select * from tableA where aaa like 'a%' and bbb ='b' and ccc like 'c%' 
      

  4.   

    前面aaa like 'a%'的%没加上,
      

  5.   

    用正则替换 String sql = "select * from tableA where aaa like 'a' and bbb ='b' and ccc like 'c'"; 
     System.out.println(sql.replaceAll("(?<= like )(.+?)", "$1%"));
    // 输出 select * from tableA where aaa like '%a' and bbb ='b' and ccc like '%c'
      

  6.   

    不好意思拷贝错了 String sql = "select * from tableA where aaa like 'a' and bbb ='b' and ccc like 'c'"; 
     System.out.println(sql.replaceAll("(?<= like ')(.+?)(?=')", "$1%"));
    // 输出select * from tableA where aaa like 'a%' and bbb ='b' and ccc like 'c%'
      

  7.   

    用正则表达式
    String regExp="(like '\\w*)(')";
    String inputText="select * from tableA where aaa like 'a' and bbb ='b' and ccc like 'c'";
    inputText=inputText.replaceAll(regExp,"$1%$2"));
      

  8.   


    //(? <= like ')(.+?)(?='):   (? <= like ')要求左边是 like ',(?=')要求右边是',(.+?)是这中间的字符
    // $1%:  $1就是(.+?)代替的字符,再加上%
    sql.replaceAll("(? <= like ')(.+?)(?=')", "$1%")); //就是将所有左边是 like ',右边是'之间的字符替换为原来中间的字符加上%