第一次到csdn的java论坛里发帖,恳求高手帮忙解决一下。现在有这样的一个字符串: [code:and m.code in ('A1','A2','A3','A(T+5)','A9')]
替换成如下形式:and m.code in ('A1','A2','A3','A(T+5)','A9')
急求高手解决。。
谢谢先了

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【lagon】截止到2008-07-08 09:19:26的历史汇总数据(不包括此帖):
    发帖的总数量:1                        发帖的总分数:60                       
    结贴的总数量:1                        结贴的总分数:60                       
    无满意结贴数:0                        无满意结贴分:0                        
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:0.00  %               无满意结分率:0.00  %                  
    敬礼!
      

  2.   

    就是去头去尾的哪些固定的字符?
    那substring就好了
      

  3.   

    如果只是为了把code:去掉的话:
     String str="code:and m.code in ('A1','A2','A3','A(T+5)','A9')";
          str=str.substring(5);
      

  4.   

    String str = String str="code:and m.code in ('A1','A2','A3','A(T+5)','A9')"; 
    str.split(":");
      

  5.   

    str=str.replaceall("[code:"," ");
    str=str.replaceall("]"," ");
    str=str.trim();
      

  6.   

    不能 用substring啊
    因为要替换的字符串是嵌在另外一个字符串里面的想用replaceall替换,但是('A1','A2','A3','A(T+5)','A9')括弧里面又有个括弧 始终是替换不掉
    不知道怎么整!
      

  7.   

    用replaceall如何替换?
      
      

  8.   


    不能用这种替换,因为要替换的字符串里面不止一个类似 [code:and m.code in ('A1','A2','A3','A(T+5)','A9')] 这种结构的,要通过判断一个一个替换掉的
      

  9.   

    没看出来: [code:and m.code in ('A1','A2','A3','A(T+5)','A9')]
    和:
    and m.code in ('A1','A2','A3','A(T+5)','A9') 
    括弧上有什么区别。
    代码:
    public class Test {    public static void main(String[] args) {
            String str = "[code:and m.code in ('A1','A2','A3','A(T+5)','A9')]";
            System.out.println(str);
            str= str.substring(6, str.length() - 1);
            System.out.println(str);
        }
    }
    结果:
    [code:and m.code in ('A1','A2','A3','A(T+5)','A9')]
    and m.code in ('A1','A2','A3','A(T+5)','A9')
    不满足你的要求吗?
      

  10.   


    不能 用substring啊 
    因为要替换的字符串是嵌在另外一个字符串里面的 想用replaceall替换,但是('A1','A2','A3','A(T+5)','A9')括弧里面又有个括弧 始终是替换不掉 
    不知道怎么整!
    各位达人有没有好的解决办法啊?
      

  11.   

    好的,问题是这样的有一个字符串a
    String a = “select m.* from m where 1=1 [code:and m.code in ('A1','A2','A3','A(T+5)','A9')] ”;现在想把a中的[code:and m.code in ('A1','A2','A3','A(T+5)','A9')] 替换成and m.code in ('A1','A2','A3','A(T+5)','A9')
    如何替换?
      

  12.   

    其实这个('A1','A2','A3','A(T+5)','A9') 是传进来的一个参数,现在定义为b
    现在就是通过这个参数b来决定如何替换的
    1.如果b为空,a =" select m.* from m where 1=1";
    2.如果b不为空,a= "select m.* from m where 1=1 and m.code in ('A1','A2','A3','A(T+5)','A9') 
    "就是这个样子的
      

  13.   


    str=str.replaceall("["," "); 
    str=str.replaceall("]"," ");
    str=str.replaceall("code:"," ");  //各种各样的类型 格式为 xxx: 可以使用switch 也可以使用全替换
    str=str.replaceall("dode:"," "); 
    str=str.replaceall("ddde:"," "); 
    ............
    str=str.trim(); 
      

  14.   

    感觉字符的替换最好就是用正则表达式来解决..这个问题还算简单可以用String 或BufferString的方法来解决..最好养成哪种习惯..多运用一下正则对你有好处.
      

  15.   


    public class aa { /**
     * @param args
     */
    private  String a = "[code:and m.code in ('A1','A2','A3','A(T+5)','A9')] ";
    private  String b = "('A1','A2','A3','A(T+5)','A9') ";
    int temp = 0;

    public  String getString()
    {

    if(b != null || b.length() <1)
    {
    a = a.replace("[", "");
    a = a.replace("]", "");

    if(a.contains("code:"))
    {
    a = a.replace("code:", "");
    }

    }
    return a.trim();
    }
    public static void main(String[] args) {
    // TODO 自动生成方法存根
    aa aa = new aa();
    System.out.println(aa.getString());
    }}
      

  16.   


    public class aa { /** 
    * @param args 
    */ 
    private  String a = "[code:and m.code in ('A1','A2','A3','A(T+5)','A9')] "; 
    private  String b = "('A1','A2','A3','A(T+5)','A9') "; 
    int temp = 0; public  String getString() 
    { if(b != null ¦ ¦ b.length() >1) 

    a = a.replace("[", ""); 
    a = a.replace("]", ""); if(a.contains("code:")) 

    a = a.replace("code:", ""); 
    } } 
    return a.trim(); 

    public static void main(String[] args) { 
    // TODO 自动生成方法存根 
    aa aa = new aa(); 
    System.out.println(aa.getString()); 
    } }改下 是 >1  ~~
      

  17.   


    请问一下这个问题 应该怎么解决?
    你能给数具体个例子吗?
     比如我现在的这个问题,该怎么解决?
    String a = "select * from m where 1=1 [code:and code in ('A1','A2','A(T+5)')]" +
    "[id:and id in ('B1','B2','B(T+5)')]";//[...]可能有多个未知的条件
    String b =  "[code:and code in ('A1','A2','A(T+5)')]";//这只是个个例,
    String c = "and code in ('A1','A2','A(T+5)')";
    String d = "('A1','A2','A(T+5)";
    String e = a.replaceAll(b,c);这种做法肯定是错的,请问你有什么好的办法可以达到我所表达出来的样子呢?
      

  18.   

    请问一下这个问题 应该怎么解决? 
    谁能给出个解决的具体例子吗? 
    比如现在的这个问题,该怎么解决? 
    String a = "select * from m where 1=1 [code:and code in ('A1','A2','A(T+5)')]" + 
    "[id:and id in ('B1','B2','B(T+5)')]";//[...]可能有多个未知的条件 
    String b =  "[code:and code in ('A1','A2','A(T+5)')]";//这只是个个例, 
    String c = "and code in ('A1','A2','A(T+5)')"; 
    String d = "('A1','A2','A(T+5)')"; 
    String e = a.replaceAll(b,c); 这种做法肯定是错的,请问你有什么好的办法可以达到我所表达出来的样子呢?
    最后得出来的结果应该是
    "select * from m where 1=1 and code in ('A1','A2','A(T+5)')
    and id in ('B1','B2','B(T+5)') and 
      

  19.   

    用正则的话,这样就可以了, public static void do9(){
    String str = "[code:and code in ('A1','A2','A(T+5)')]";
    Pattern pattern = Pattern.compile("\\[code:([^\\]]+)\\]",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
    Matcher m = pattern.matcher(str);
    while(m.find()){
    System.out.println(m.group(1));
    }
    }
      

  20.   

    想直接换的话就这样写: public static void do5(){
    String str = "[code:and code in ('A1','A2','A(T+5)')]";
    str=str.replaceAll("\\[code:", "").replaceAll("\\]", "");
    System.out.println(str);
    }
      

  21.   


    忘记说了一句,还有一种情况就是and code in ('A1','A2','A(T+5)'),括弧中的数据有可能为空
    如果为空的话,就要把"[code:and code in ('A1','A2','A(T+5)')]"整个替换掉,而且code也不是个固定的字符串,可能为其他的字符如id,name
      

  22.   

    发现我想的跟你们的有点出入
    我是想"[code:and code in ('A1','A2','A(T+5)')]"整个替换掉
    而你们只是想如何把[code:。]去掉唉,是我表达的不够清楚的原因?
      

  23.   

    楼主之前也没有说明白啊, public static void do9(){
    String str = "[code:and code in ('A1','A2','A(T+5)')]";
    Pattern pattern = Pattern.compile("\\[.*?(\\(.*\\))\\]",Pattern.CASE_INSENSITIVE|Pattern.DOTALL);
    Matcher m = pattern.matcher(str);
    while(m.find()){
    if(m.group(1)!=null && m.group(1)!=""){
    System.out.println("and m.code in "+m.group(1));//非空
    }
    else{
    //为空处理
    }

    }
    }
      

  24.   

    public class Test01 {    public static void main(String[] args) {
            String str = "select m.* from m where 1=1 [code:and m.code in ('A1','A2','A3','A(T+5)','A9')]";
            String regex = "\\[code:([^\\]]*)\\]";
            str = str.replaceAll(regex, "$1");
            System.out.println(str);
        }
    }
      

  25.   

    没怎么看明白楼主的要求前面我看到的还是 [code:and m.code in ('A1','A2','A(T+5)')] 这样的,
    怎么到后面成了 [code:and code in ('A1','A2','A(T+5)')] 这样的?另外,[code: 与 and 后面的 code 有什么关系?
      

  26.   

    假设 [code: 与 and 后面的 code 是相同的,并且 and 后面的 code 前可以加上表名 m.code 这样的话。
    public class Test01 {    public static void main(String[] args) {
            String str1 = "select * from m where 1=1 [code:and code in ('A1','A2','A(T+5)')]" + 
                         "[id:and id in ('B1','B2','B(T+5)')]";
            String regex = "\\[(.*?):\\s*(and\\s+(?:[^\\] ]\\.)*\\1[^\\]]*)\\]";
            str1 = str1.replaceAll(regex, " $2");        
            System.out.println(str1);
            
            String str2 = "select * from m where 1=1 [code:and m.code in ('A1','A2','A(T+5)')]" + 
                          "[id:and m.id in ('B1','B2','B(T+5)')]";
            str2 = str2.replaceAll(regex, " $2");        
            System.out.println(str2);
        }
    }