使用java原生String.split()方法
String[] splitted = "peter,james,thomas".split(","); String[] splitted = "car jeep scooter".split(" "); String[] splitted = "192.168.1.178".split("\\.")
String[] splitted = "b a, e, l.d u, n g".split("\\s+|,\\s*|\\.\\s*"));
|
使用工具类Common包下的StringUtils.split()方法
String[] splitted = StringUtils.split("car jeep scooter");
|
使用guava工具包的Splitter流
List<String> resultList = Splitter.on(',') .trimResults() .omitEmptyStrings() .splitToList("car,jeep,, scooter");
|
总结: String成员方法分割字符串处理简单分割比较容易,但是复杂一点使用工具类还是比较方法. 预先善其事必先利其器,多学习一些工具类的使用,提高开发效率也还不错.