首页 > 解决方案 > 制作切割字符串android的算法

问题描述

我收到了一个电车(字符串内容一些信息(产品和价格),例如“/tomatos*500/botot*1200/tor*1200”),我需要将该字符串剪切成 ArrayList 和产品内容(字符串产品, 字符串价格)。

这是一个数据样本

"/香蕉*200 /西红柿*7850 /福尔福尔*10 /鸡蛋*1200 /猕猴桃*100 "

我试过这个算法:

 String[] decouped_product = post.toString().split("/");
                for(int i = 0 ; i <decouped_product.length;i++){
                    Log.d("decoupage1",decouped_product[i]);
                    if(decouped_product[i].equals("")){

                    }else {
                        String[] decouped_prix = post.toString().split("\\*");
                        String Product_nom = decouped_prix[0];
                        String Prix = decouped_prix[1];
                        Log.d("nom: ",Product_nom);
                        Log.d("prix: ",Prix);
                    }

                }
                Log.d("changed file",post.toString());

但它不起作用它给了我

别名:西红柿

价格:*500/botot

类似的东西。但我希望它像:

别名:西红柿

价格:500

名词:底

价格:1200

名词:托

价格:1200

标签: androidstringalgorithm

解决方案


解决方案是:

 String[] decouped_product = post.toString().split("/");
                for(int i = 0 ; i <decouped_product.length;i++){
                    Log.d("decoupage1",decouped_product[i]);
                    if(decouped_product[i].equals("")){

                    }else {
                        String[] decouped_prix = decouped_product[i].toString().split("\\*");
                        String Product_nom = decouped_prix[0];
                        String Prix = decouped_prix[1];
                        Log.d("nom: ",Product_nom);
                        Log.d("prix: ",Prix);
                    }

                }
                Log.d("changed file",post.toString());

推荐阅读