从逗号分隔的String创建ArrayList,不带空格

时间:2017-07-27 02:35:30

标签: java arrays string arraylist

假设我有一个随机放置空格的字符串和其他字符周围的逗号。

[item1, item2, item3]

如何使用

等项目获取ArrayList
select      name
           ,min(orderno)    as orderno
           ,productcategory
           ,sum(amount)     as amount
           ,max(named_struct('orderno',orderno,'description',description)).description

from        mytable

group by    name
           ,productcategory
;

4 个答案:

答案 0 :(得分:3)

使用以下内容:

String str = "item1 , it em 2,  ite m 3"
String[] splitArray = str.split(",");
ArrayList<String> list = new ArrayList<String>();
for (String s:splitArray)
{
    list.add(s.replace(" ", ""));
}

答案 1 :(得分:0)

只是为了好玩:-)

import org.apache.commons.lang3.StringUtils;

String inputString = "item1 , it em 2,  ite m 3";
List<String> stringList = Arrays.stream(StringUtils.deleteWhitespace(inputString).split(",")).collect(Collectors.toList());

答案 2 :(得分:0)

这是另一个解决方案

    //Given string
    String str = "item1 , it em 2,  ite m 3";

    //Match one or more occurrence of space in the string and replace by empty
    String pattern = "\\s+";
    str = str.replaceAll(pattern, "");

    //Split by , and convert to ArrayList
    List<String> items = Arrays.asList(str.split(","));

    for(String temp: items)
        System.out.println(temp);

答案 3 :(得分:0)

使用Java 8的parallelSetAll

String str = "item1 , it em 2,  ite m 3";
String[] stringlist = str.split(",");
Arrays.parallelSetAll(stringlist, (i) -> stringlist[i].replaceAll(" ",""));