public static void main(String[] args) {
String urlParam="&cateid=36&local=1&isbiz=0&state=1&t=1314852427";  //参数个数不限
String appendCondition="state=2&ac=1&a=2&ccd=3&c=3"; ////参数个数不限(有urlParam中相同的也有不同的)
System.out.println("处理前:"+urlParam);
String[] acAry = appendCondition.split("&");
for (String item : acAry) {
if (item != null && item.indexOf("=") > 0) {
String[] itemAry = item.split("="); // item[0]="state=2"
if (urlParam.indexOf(itemAry[0].toLowerCase()) > 0) { //itemAry[0].toLowerCase() + "="
String regex="&" + itemAry[0].toLowerCase() + "=[^&]*"; // itemAry[0]="&state=2"
Pattern pattern = Pattern.compile(regex);
Matcher matcher=pattern.matcher(urlParam);
if(matcher.find()){
String gr=matcher.group();
System.out.println("找到: "+gr);
urlParam=urlParam.replaceFirst(gr, "&" + item);
}else{
System.out.println("未找到1:"+item);
urlParam += "&" + item;
}
}else{
System.out.println("未找到2:"+item);
urlParam += "&" + item;
}
}
}
System.out.println("处理后:"+urlParam);
}注释虽然不多,但要点内容都基本描述了。欢迎各位拍砖!!

解决方案 »

  1.   


    LinkedHashMap<String, String> hm = new LinkedHashMap<String, String>();
    StringBuffer sb = new StringBuffer();
    String urlParam = "&cateid=36&local=1&isbiz=0&state=1&t=1314852427"; // 参数个数不限
    Matcher m = Pattern.compile("(\\w*)=(\\w*)").matcher(urlParam);
    while (m.find())
        if (hm.put(m.group(1), m.group(2)) == null)
    sb.append("&" + m.group(1) + "=" + m.group(2));
    String appendCondition = "state=2&ac=1&a=2&ccd=3&c=3"; // //参数个数不限(有urlParam中相同的也有不同的)
    m = Pattern.compile("(\\w*)=(\\w*)").matcher(appendCondition);
    while (m.find())
        if (hm.put(m.group(1), m.group(2)) == null)
    sb.append("&" + m.group(1) + "=" + m.group(2));
    /*
    System.out.println("处理前:" + urlParam);
    String[] acAry = appendCondition.split("&");
    for (String item : acAry) {
        if (item != null && item.indexOf("=") > 0) {
    String[] itemAry = item.split("="); // item[0]="state=2"
    if (urlParam.indexOf(itemAry[0].toLowerCase()) > 0) { // itemAry[0].toLowerCase()
    // + "="
        String regex = "&" + itemAry[0].toLowerCase() + "=[^&]*"; // itemAry[0]="&state=2"
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(urlParam);
        if (matcher.find()) {
    String gr = matcher.group();
    System.out.println("找到: " + gr);
    urlParam = urlParam.replaceFirst(gr, "&" + item);
        } else {
    System.out.println("未找到1:" + item);
    urlParam += "&" + item;
        }
    } else {
        System.out.println("未找到2:" + item);
        urlParam += "&" + item;
    }
        }
    }
    */
    System.out.println("处理后:" + urlParam);
    System.out.println("处理后:" + sb);
      

  2.   

     if(matcher.find()){
                            String gr=matcher.group();
                            System.out.println("找到: "+gr);
                            urlParam=urlParam.replaceFirst(gr, "&" + item);
                        }
    既然已经找到了,那么就在if语句块的最后一句加上break;退出循环吧!
     if(matcher.find()){
                            String gr=matcher.group();
                            System.out.println("找到: "+gr);
                            urlParam=urlParam.replaceFirst(gr, "&" + item);
                            break;
                        }
      

  3.   

    这些先用VS自带的代码分析工具看看(启用所有规则)
    或者安装Microsoft FxCop 10.0对IL代码进行分析优化。
      

  4.   

    补充:
    既然你每次都要itemAry[0].toLowerCase()
    不如String[] acAry = appendCondition.split("&");的时候就全部转换
      

  5.   

    加些辅助结构吧 
    首先把第一个字符串解析后存在MAP里,然后解析第二个更新MAP,最后遍历MAP,生成字符串就可以了