It splits a string into parts based on a delimiter. Its only small flaw is that when 2 or more delimiters sit next to each other, it will never give you an empty string. Sun’s documentation says nothing about this little detail.
The class lives in the package java.util, so you first need to import it if you want to refer to it by its short name:
import java.util.*;Let’s create the variables the class will work with:
String delimiter = "|" ;
String contentToSplit = "Hello|World|Java" ;In a loop, I go through all the tokens.
For the loop condition, the method hasMoreTokens comes in handy; it returns true if there is still a token left in the stack. The method nextToken then returns it as a String and moves the cursor in front of the next token or to the end of the string.
while (tokens.hasMoreTokens())
{
//-Can take a delimiter argument; the method then returns everything up to the first occurrence of that delimiter in this token
String token = tokens.nextToken();
}If you used an infinite loop, the nextToken() method would throw a NoSuchElementException after the last token.