且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将逗号分隔的字符串转换为列表?

更新时间:2023-09-06 19:47:31

将逗号分隔的字符串转换为列表

List<String> 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.

请注意,这只是返回一个数组的包装器:您不能例如从结果.remove()列表.对于实际的 ArrayList,您必须进一步使用 new ArrayList.

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<String>.