字符串拼接的时候,如果字母相同就让前面的数字相加
类似这种
"1PALLET,
5PACKAGE,
2PALLET,
1PALLET,
4BOX,
4PACKAGE,
1PALLET,
1PACKAGE,
28CARTON"
"5PALLET,
10PACKAGE,
4BOX,
28CARTON"

解决方案 »

  1.   

    帮你写了一下:
            HashMap<String, Integer> map = new HashMap<String, Integer>();        String str = "1PALLET,5PACKAGE,2PALLET,1PALLET,4BOX,4PACKAGE,1PALLET,1PACKAGE,28CARTON,5PALLET,10PACKAGE,4BOX,28CARTON";
            Pattern reg = Pattern.compile("(\\d+)([^,\\n]+)");
            Matcher m1 = reg.matcher(str);
            while(m1.find()){
                //System.out.println(m1.start() + ":" + m1.end());
                int num = Integer.parseInt(m1.group(1));
                String key = m1.group(2);
                Integer tmp = map.put(key, num);
                if(tmp != null)
                    map.put(key, num + tmp);
            }
            for (String key: map.keySet()) {
                System.out.println(key + ":" + map.get(key));
            }输出结果:
    CARTON:56
    PACKAGE:20
    BOX:8
    PALLET:10
      

  2.   

    如果后面括号里面有内容的话,对应的也得也加来怎么弄啊  大佬"1PALLET(1PACKAGE,1PALLET,1BOX),
    5PACKAGE,
    2PALLET,
    1PALLET,
    4BOX,
    4PACKAGE,
    1PALLET(2PALLET,1BOX),
    1PACKAGE,
    28CARTON""5PALLET(3PALLET,1BOX),
    10PACKAGE,
    4BOX,
    28CARTON" 
      

  3.   

    将拼接好的string给楼上回复的str
    String str1="";
    String str2=""; 
    String str=test(str1)+test(str2);private String test(String str) {
    String tempStrNew="";
    String strNew[]=str.split(","); 
    for(int i=0;i<strNew.length;i++){  
          if(strNew[i].indexOf('(')>=0){
          String  tempStr =strNew[i].substring(strNew[i].indexOf("(") + 1, strNew[i].lastIndexOf(")"));
          String strNewSun[]=tempStr.split(",");
          for(int j=0;j<strNewSun.length;j++){
          tempStrNew+=strNewSun[j];
          }
          }else{
           tempStrNew+=strNew[i];
          }
    }
    return tempStrNew;
    }
      

  4.   

    看错问题了,用这个
    String str1="";
    String str2=""; 
    String testString=test(str1)+test(str2);private String test(String str) {
    String tempStrNew="";
    String strNew[]=str.split(","); 
    for(int i=0;i<strNew.length;i++){  
          if(strNew[i].indexOf('(')>=0){
           String  tempStr =strNew[i].substring(strNew[i].indexOf("(") + 1, strNew[i].length()); 
           tempStrNew+=tempStr; 
          }if(strNew[i].indexOf(')')>=0){
           String  tempStr =strNew[i].substring(0, strNew[i].lastIndexOf(")"));
           tempStrNew+=tempStr; 
          }
          else{
           tempStrNew+=strNew[i];
          }
    }
    return tempStrNew;
    }
      

  5.   

    private String test(String str) {
    String tempStrNew="";
    String strNew[]=str.split(","); 
    for(int i=0;i<strNew.length;i++){  
          if(strNew[i].indexOf('(')>=0){
           String strNew1[]=str.split("("); 
           for(int j=0;j<strNew1.length;j++){
               tempStrNew+=strNew1[j];
           } 
          }if(strNew[i].indexOf(')')>=0){
           String strNew2[]=str.split(")"); 
           for(int j=0;j<strNew2.length;j++){
               tempStrNew+=strNew2[j];
           }
          }
          else{
           tempStrNew+=strNew[i];
          }
    }
    return tempStrNew;
    }