如何用''分隔字符串并在java中存储在二维数组中

时间:2016-08-17 19:44:27

标签: java

如何用''分隔后面的字符串并存储在java中的二维数组

“(''1494576','16268568','5150022241','面粉','Pillsbury','2 For $ 5.00','5','8/20/2016','80','Best All目的未漂白富集','Demoulas / Market Basket','2','itemlink')“

我只需要在''二维数组之间的数据 喜欢 温度[0] [0] = “1494576” temp [0] [1] =“16268568”

2 个答案:

答案 0 :(得分:0)

正如@Jesse Amano所建议的那样,你可能会使用JSON。这是另一种粗暴的做法:

    String s = "('1494576','16268568','5150022241','Flour','Pillsbury','2 For $5.00','5','8/20/2016','80','Best All Purpose Unbleached Enriched','Demoulas/Market Basket','2','itemlink')";
    String[][] temp = ...;
    boolean inside = false;
    boolean end = false;
    int index = 0;
    for(int i = 0; i < s.length(); i ++){
        if(s.substring(i, i + 1).equals("'")){
            inside = !inside; //determines if we are between ''
            if(end) index ++;
            end = !end; //determines if this is the closing '
        }
        else{
            if(inside) temp[0][index] += s.substring(i, i +1);
        }
}

答案 1 :(得分:0)

String s = "('1494576','16268568','5150022241','Flour','Pillsbury','2 For $5.00','5','8/20/2016','80','Best All Purpose Unbleached Enriched','Demoulas/Market Basket','2','itemlink')"
s  = s.substring(2,s.length()-2);
String[] str = s.split("','");
String[][] temp = new String[3][4];
int k = 0;
for(String[] a: temp){
    for(String b: a){
        b = str[k];
        k++;
    }
}

由于您有13个元素,因此无法将其放入矩形2D数组中。这个只能容纳其中的12个。您可以调整数据或使用锯齿状数组只保留13个。