字符串相加

qq_43439910 2019-03-28 02:54:14

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


"5PALLET,
10PACKAGE,
4BOX,
28CARTON"
...全文
264 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_43439910 2019-03-29
  • 打赏
  • 举报
回复
如果后面括号里面有内容的话,对应的也得也加来怎么弄啊 大佬

"1PALLET(1PACKAGE,1PALLET,1BOX),
5PACKAGE,
2PALLET,
1PALLET,
4BOX,
4PACKAGE,
1PALLET(2PALLET,1BOX),
1PACKAGE,
28CARTON"

"5PALLET(3PALLET,1BOX),
10PACKAGE,
4BOX,
28CARTON"
qq_37810333 2019-03-29
  • 打赏
  • 举报
回复
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; }
qq_37810333 2019-03-29
  • 打赏
  • 举报
回复
看错问题了,用这个 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; }
qq_37810333 2019-03-29
  • 打赏
  • 举报
回复
将拼接好的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; }
游北亮 2019-03-28
  • 打赏
  • 举报
回复
帮你写了一下:


        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

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧