我有一个长度未知的字符串,看起来像这样
"dog, cat, bear, elephant, ..., giraffe"
用逗号分隔这个字符串的最佳方法是什么,这样每个单词都可以成为数组列表的一个元素?
例如
List<String> strings = new ArrayList<Strings>();
// Add the data here so strings.get(0) would be equal to "dog",
// strings.get(1) would be equal to "cat" and so forth.
在构建。gradle加番石榴
compile group: 'com.google.guava', name: 'guava', version: '27.0-jre'
然后
public static List<String> splitByComma(String str) {
Iterable<String> split = Splitter.on(",")
.omitEmptyStrings()
.trimResults()
.split(str);
return Lists.newArrayList(split);
}
public static String joinWithComma(Set<String> words) {
return Joiner.on(", ").skipNulls().join(words);
}
享受:)