Java 8 | StringTokenizer
StringTokenizer — One of the Java util package that breaks a String into tokens.
Hi y’all! Today I’m going to show you how to use the StringTokenizer. It’s very similar with one of the String method split(), but you might find StringTokenizer more convenient to use.
Unlike the split() method, the StringTokenizer is a class, so you have to create it with the new operator.
I declared the String named test, and put it as the first parameter of the StringTokenizer, and than at the second parameter I put the slash “/”.
So it will split the String test by the slashes, and store each of the words into tokens. The way to take those tokens out is like below.
Like this, we can use the nextToken() method to take out the token, but the tokens that you took out once, is going to removed. And also if I try to take more tokens than it actually has, there will be an error like below.
To avoid this, it’s better to get the numbers of the token first. We can do that with the countTokens() method.
Well that’s all for today, thank you for reading my post again! See ya!