What is StringTokenizer in Java?

StringTokenizer class is used for creating tokens in Java. It allows an application to break or split into small parts. Each split string part is called Token.

StringTokennizer in Java, object keeps the string in the present position as it is to be tokenized. By taking a substring of the string a token can return that utilize to make the StringTokenizer protest.

To get in-Depth knowledge on Java you can enroll for a live demo on Java Online Training

StringTokenizer Constructors

There are 3 types of Constructors available in Java StringTokenizer, let’s discuss them with their examples-

  • (String str)

str is a string to be tokenized and it considers default delimiters like newline, space, tab, carriage return and form feed which can be further tokenized.

  • StringTokenizer(String str, String delim)

delim is set of delimiters that are used to tokenize the given string.

Take your career to new heights of success with Java Training

  • StringTokenizer(String str, String delim, boolean flag)

Since the first two parameters have the same meaning. The flag serves the following purpose. If the flag is false, delimiter characters serve to separate tokens and if the flag is true, delimiter characters are considered to be tokens

Example 

package com.onlineitguru.StringTokenizer;
import java.util.*;
public class StringTokenizerDemo {
  public static void main(String args[])
  {
    System.out.println("StringTokenizer Constructor 1 - ");
    StringTokenizer st1 =
        new StringTokenizer("Hello Readers, Welcome to DataFlair", " ");
    while (st1.hasMoreTokens())
      System.out.println(st1.nextToken());
    System.out.println("StringTokenizer Constructor 2 - ");
    StringTokenizer st2 =
        new StringTokenizer("JAVA : Code : String", " :");
    while (st2.hasMoreTokens())
      System.out.println(st2.nextToken());
    System.out.println("StringTokenizer Constructor 3 - ");
    StringTokenizer st3 =
        new StringTokenizer("JAVA Code String", " : ",  true);
    while (st3.hasMoreTokens())
      System.out.println(st3.nextToken());
  }
}

StringTokenizer Methods

Following are 5 types of Methods available in Java StringTokenizer:

1. hasMoreTokens()

The method java.util.StringTokenizer.hasmoreTokens() plays a role in testing, if tokens are present for the StringTokenizer’s string.

Basically, those characters that are considered to be delimiters by the StringTokenizer object are changed to characters in the string delimiter. Then the next token to the current position in the string is returned.

To learn more about StringTokenizer in Java and other great features of Java, you can enroll for alive demo on java online course

Syntax

public boolean hasMoreTokens()

Returns: True if and only if next token to the current position in the string exists, else false.

2. nextToken()

The method java.util.StringTokenizer.nextToken() returns next token from the given StringTokenizer.

Syntax

public String nextToken()
  • Return: The next token from the given StringTokenizer if present.
  • Throws: NoSuchElementException – if no more token are left.

3. countTokens()

This method java.util.StringTokenizer.countTokens() returns  total number of tokens which are present.

Hence, the number further use nextToken() method before it gives an exception and use it.

Syntax

public int countTokens()

Return: The number of tokens remaining in the string using the current delimiter set.
Example 

package com.onlineitguru.StringTokenizer;
import java.util.*;
public class StringTokenizerMethods {
   public static void main(String args[])
     {
         String mydelim = " : ";
         String mystr = "JAVA : Code : String : Tokenizer : Dataflair";
         StringTokenizer flair3 =
                           new StringTokenizer(mystr, mydelim);
         int count = flair3.countTokens();
         for (int i = 0; i <count; i++)
             System.out.println("token at [" + i + "] : "
                                + flair3.nextToken());
         StringTokenizer string1 = null;
    while (flair3.hasMoreTokens())
             System.out.println(string1.nextToken());
     }
  }

4. nextElement()

This method returns Object rather than String and java.util.StringTokenizer.nextElements() works similar to nextToken exists so that this class can implement the Enumeration interface.

Syntax 

public Object nextElement()

Return: The next token from the given StringTokenizer.
Throws: NoSuchElementException – if there are no more tokens left.

5. hasMoreElements()

Next, in this method java.util.StringTokenizer.hasMoreElements() returns same value as hasMoreToken.

Syntax 

public boolean hasMoreElements()

Return: True if tokens are present in the string, else false.

Example 

package com.DataFlair.StringTokenizer;
import java.util.*;
public class StringTokenizerMethod
{
   public static void main(String args[])
   {
       String mydelim = " : ";
       String mystr = "JAVA : Code : String : Tokenizer : Flair";
       StringTokenizer stringName =
                     new StringTokenizer(mystr, mydelim);
       int count = stringName.countTokens();
       System.out.println("Number of tokens : " + count);
       while (stringName.hasMoreElements())
           System.out.println(stringName.nextElement());
   }
}

Summary

So, now you know why we use StringTokenizer. Ther are various constructors and methods for breaking the string into tokens. Hope, you understood everything with examples.

Published by sindhuja cynixit

i am working as a digital marketing executive

Leave a comment

Design a site like this with WordPress.com
Get started