The java.util.StringTokenizer class allows you to break a string into tokens. It is simple way to break string.
The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a pertoken basis.
StringTokenizer Split by space
import java.util.StringTokenizer; public class Simple{ public static void main(String args[]){ StringTokenizer st = new StringTokenizer("apple ball cat dog"," "); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } } }
Output:
apple
ball
cat
dog
Related Article: How To Split Strings in Java
StringTokenizer Split by comma ‘,’
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer(“apple,ball cat,dog”, “,”);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
Output:
apple
ball cat
dog
Splitting a string into fixed length parts
Break a string up into substrings all of a known length
The trick is to use a look-behind with the regex \G, which means “end of previous match”:
String[] parts = str.split("(?<=\G.{8})");
The regex matches 8 characters after the end of the last match. Since in this case the match is zero-width, we could more simply say “8 characters after the last match”.
Conveniently, \G is initialized to start of input, so it works for the first part of the input too.
Break a string up into sub strings all of variable length
Same as the known length example, but insert the length into regex:
int length = 5;
String[] parts = str.split("(?<=\G.{" + length + "})");