Java StringTokenizer

StringTokenizer class breaks a string into tokens using a specified delimiter.

Note: space is the default delimiter.

 

Constructors of StringTokenizer class:

  1. public StringTokenizer(String str): creates a string tokenizer for the specified string. By default, space is taken as a delimiter here.
  2. public StringTokenizer(String str, String delimiter): creates a string tokenizer for the specified string using a specified delimiter. Delimiter characters will be treated as separators only and not be treated as tokens.
  3. public StringTokenizer(String str, String delimiter, boolean returnDelimiters): creates a string tokenizer for the specified string by using a specified delimiter. Delimiter character can be treated as separator returnDelimiters is true.

 

Commonly used methods of StringTokenizer:

1. hasMoreTokens(): checks whether more tokens are available. It returns true if more tokens are available else returns false.

Syntax:

public boolean hasMoreTokens()

 

2. hasMoreElements():  same as hasMoreTokens() , mainly used in case of enumeration.

Syntax:

public boolean hasMoreElements()

 

3. nextToken(): returns the next token from this string tokenizer. Note: It will throw NoSuchElementException if no more token is available.
Syntax:

public String nextToken()

 

4. nextToken(String delimiter): returns the next token from this string tokenizer using a specified delimiter. It is mainly used when we want to separate tokens using different delimiters in a single-string tokenizer.

Syntax:

public String nextToken(String delim)

Note: It will throw NoSuchElementException if no more token is available.

 

5. nextElement(): same as nextToken() except that it returns an object rather than string. It is mainly used in the case of enumeration.

Syntax:

public Object nextElement()

Note: It will throw NoSuchElementException if no more token is available.

 

6. countTokens(): returns the number of times the nextToken method can be called on this string tokenizer before an exception occurs.

Syntax:

public int countTokens()

 

Example 1:

class TestStringTokenizer{
    //By default whitespace will act as separator.
    StringTokenizer str = new StringTokenizer("Hello i am here");
    
    public void displayTokens(){
        while(str.hasMoreTokens()){
            System.out.println(str.nextToken());
        }
    }
}

public class StringTokenizerExample {
    public static void main(String args[]){
        //creating TestStringTokenizer object.
        TestStringTokenizer obj = new TestStringTokenizer();
        
        //method call
        obj.displayTokens();		
    }
}

Output

Hello 
i 
am 
here

 

Example 2:

class TestStringTokenizer{
    //Using comma as separator.
    StringTokenizer str = new StringTokenizer("Hello ,i ,am ,here", ",");

    public void displayTokens(){
        while(str.hasMoreTokens()){
            System.out.println(str.nextToken());
        }
    }
}

public class StringTokenizerExample {
    public static void main(String args[]){
        //creating TestStringTokenizer object.
        TestStringTokenizer obj = new TestStringTokenizer();
        
        //method call
        obj.displayTokens();		
    }
}

Output

Hello
i
am
here

 

Example 3:

class TestStringTokenizer{
    //Using multiple separators.
    StringTokenizer str = new StringTokenizer("Hello ,i ;am :here", ", ; :");
    
    public void displayTokens(){
        while(str.hasMoreTokens()){
            System.out.println(str.nextToken());
        }
    }
}

public class StringTokenizerExample {
    public static void main(String args[]){
        //creating TestStringTokenizer object.
        TestStringTokenizer obj = new TestStringTokenizer();
        
        //method call
        obj.displayTokens();		
    }
}

Output

Hello
i
am
here