Buzz Code
Published in

Buzz Code

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.

[StringTokenizer]

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.

Like this!

Well that’s all for today, thank you for reading my post again! See ya!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store