public class SplitterTest { public static void main(String args[]){ //1、拆分字符串为List集合 String str="小小,妈妈,爸爸,爷爷,奶奶"; Splitter splitter =Splitter.on(","); List<String> list=splitter.splitToList(str); System.out.println(list); //输出:[小小, 妈妈, 爸爸, 爷爷, 奶奶] //2、忽略空字符 String str1="a,b,c,d,,f,g"; //omitEmptyStrings()代表去除空字符串 List<String> list1=Splitter.on(",").omitEmptyStrings().splitToList(str1); System.out.println(list1); //输出:[a, b, c, d, f, g] //3、忽略空字符且去除字符串前后空格 String str2="a,b,c,d,,f, g "; List<String> list2=Splitter.on(",").omitEmptyStrings().trimResults().splitToList(str2); System.out.println(list2); //输出:[a, b, c, d, f, g] } }
原文地址:https://www.cnblogs.com/jiaowoxiaofeng/p/11966707.html
时间: 2024-11-06 03:42:14