如果字符串数组包含值,如何获取它的某个部分

时间:2016-04-09 23:27:24

标签: java android

String sArr = "ads,Distribution;8,0.1%;10,2.6%;15,2.3%";
String sStrip = sArr.replace(" ", "").split(";");
string sAp = {"7","8","9","10","11"};

boolean blC = Arrays.asList(sStrip).contains(sAp[1]); //this returns false...
String getPerc = ""; //If I use `.contains(sAp[3])` should return `2.6%`

这是一个示例app伪代码:

for each `sAp`
     check to see if the `sAp` value exist in `sStrip`[0].
          if `sAp` exist in `sStrip`[0]
               get the second part of the found match

`sApp` = 7
`sStrip` doesn't have a 7... do nothing

`sApp` = 8
`sString does have a 8, return 0.1%

...

如何获取getPerc

中的值

尝试了这个,没有工作:

for(String s : sStrip) {
    if (sAp[3].contains(s)) {
        tes = s.split(",")[1];
    }
    else {
        tes = "0%";
    }
}

sAp[i]sStrip的第一部分匹配。所以ads,8,10,15不是Distribution,0.1%,2.6%,2.3%

1 个答案:

答案 0 :(得分:0)

试试这个:

    String sArr = "ads,Distribution;8,0.1%;10,2.6%;15,2.3%";
    String[] sStrip = sArr.split(";");
    String[] sAp = {"7", "8", "9", "10", "11"};

    List<String> result = new ArrayList<>();
    for (int i = 0; i < sStrip.length; i++) {
        for (int j = 0; j < sAp.length; j++) {
            if (sStrip[i].contains(sAp[j])) {
                String value = sStrip[i].substring(sStrip[i].indexOf(",") + 1);
                result.add(value);
                break;
            }
        }
    }

在这种情况下,输出为:0.1%, 2.6%