Java StringTokenizer - nextToken() Method
The java.util.StringTokenizer.nextToken() method returns the next token in this string tokenizer's string. First, the set of characters considered to be delimiters by this StringTokenizer object is changed to be the characters in the string delim. Then the next token in the string after the current position is returned. The current position is advanced beyond the recognized token. The new delimiter set remains the default after this call.
Syntax
public String nextToken(String delim)
Parameters
delim |
Specify the new delimiters. |
Return Value
Returns the next token, after switching to the new delimiter set.
Exception
- Throws NoSuchElementException, if there are no more tokens in this tokenizer's string.
- Throws NullPointerException, if delim is null.
Example:
In the example below, the java.util.StringTokenizer.nextToken() method returns the next token and change the delimiters of the given string tokenizer.
import java.util.*; public class MyClass { public static void main(String[] args) { //creating a string tokenizer with comma (,) as delimiter StringTokenizer st = new StringTokenizer("Alpha#Coding#Skills",","); //changing the delimiter to "#" //and returning the next token System.out.println("StringTokenizer contains: "); System.out.println(st.nextToken("#")); //printing next tokens of the string tokenizer while(st.hasMoreTokens()) System.out.println(st.nextToken()); } }
The output of the above code will be:
StringTokenizer contains: Alpha Coding Skills
❮ Java.util - StringTokenizer