To summarize: there are at least five ways to split a string in Java:
String.split():
String[] parts ="10,20".split(",");Pattern.compile(regexp).splitAsStream(input):
List<String> strings = Pattern.compile("\\|") .splitAsStream("010|020202") .collect(Collectors.toList());StringTokenizer (legacy class):
StringTokenizer strings = new StringTokenizer("Welcome to EXPLAINJAVA.COM!", "."); while(strings.hasMoreTokens()){ String substring = strings.nextToken(); System.out.println(substring); }Google Guava Splitter:
Iterable<String> result = Splitter.on(",").split("1,2,3,4");Apache Commons StringUtils:
String[] strings = StringUtils.split("1,2,3,4", ",");
So you can choose the best option for you depending on what you need, e.g. return type (array, list, or iterable).
HereHere is a big overview of these methods and the most common examples (how to split by dot, slash, question mark, etc.)