Adding comma separated strings to an ArrayList and vice versa

前端 未结 11 1253
旧巷少年郎
旧巷少年郎 2021-01-01 05:18

How to add a comma separated string to an ArrayList? My string \"returnedItems\" could hold 1 or 20 items which I\'d like to add to my ArrayList \"selItemArrayList\".

<
11条回答
  •  感动是毒
    2021-01-01 06:03

    String returnedItems = "a,b,c";
    List sellItems = Arrays.asList(returnedItems.split(","));
    

    Now iterate over the list and append each item to a StringBuilder:

    StringBuilder sb = new StringBuilder();
    for(String item: sellItems){
        if(sb.length() > 0){
            sb.append(',');
        }
        sb.append(item);
    }
    String result = sb.toString();
    

提交回复
热议问题