更新时间:2023-09-06 20:08:40
将逗号分隔的字符串转换为列表
List<String> items = Arrays.asList(str.split("\\s*,\\s*"));
上面的代码在定义为zero or more whitespace, a literal comma, zero or more whitespace
的定界符上拆分字符串,它将把单词放入列表并折叠单词和逗号之间的所有空格.
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.
请注意,这仅返回数组的包装器:例如,您 不能 来自结果List
的.remove()
.对于实际的ArrayList
,您必须进一步使用new ArrayList<String>
.
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>
.