How to convert comma-separated String to List?

前端 未结 24 2314
你的背包
你的背包 2020-11-22 16:58

Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for t

24条回答
  •  自闭症患者
    2020-11-22 17:21

    Convert comma separated String to List

    List items = Arrays.asList(str.split("\\s*,\\s*"));
    

    The above code splits the string on a delimiter defined as: zero or more whitespace, a literal comma, zero or more whitespace which will place the words into the list and collapse any whitespace between the words and commas.


    Please note that this returns simply a wrapper on an array: you CANNOT for example .remove() from the resulting List. For an actual ArrayList you must further use new ArrayList.

提交回复
热议问题