list和以逗號分割的String相互轉換(guava)

代碼以下:javascript

import com.alibaba.fastjson.JSON;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.base.Splitter;

   List<String> list = Lists.newArrayList("a","b","c"," ");
   String s = Joiner.on(",").join(list); // 以逗號分割
   System.out.println(s); // a,b,c, 
   // 去先後空格&&去空string
   Splitter split = Splitter.on(',').trimResults().omitEmptyStrings();
   List<String> list1 = split.splitToList(s);
   System.out.println(JSON.toJSONString(list1)); // ["a","b","c"]
相關文章
相關標籤/搜索