Home » Core Java » util » StringTokenizer » Java StringTokenizer Example

About Katerina Zamani

Katerina Zamani
Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.

Java StringTokenizer Example

Java supports StringTokenizer class, from where we can split a string into tokens. In this example we are going to show the use of StringTokenizer class, as well as some of its basic operations.

1. Syntax of the constructors

Depending on our application, we can choose one of the below constructors for StringTokenizer class.

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

The possible arguments that are used are:

  • str: is the string we want to split into tokens
  • delim: the defined delimiter of token separation (space is the default one)
  • returnDelims: declares whether to include the delimiter as a returned token (true value) or skip the delimiter (false value)

2. Example of StringTokenizer in Java

Create a new java file with the name StringTokenizerClass and paste the following code.

StringTokenizerClass.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.javacodegeeks.core.stringtokenizer;
 
import java.util.StringTokenizer;
 
public class StringTokenizerClass {
 
    public static void main(String[] args) {
        String str = "Java Code Geeks Examples !";
        String s = "Java-Code--Geeks-Examples-!";
     
        StringTokenizer strtokenizer = new StringTokenizer(str);
         
        // loop in order to take all tokens
        while(strtokenizer.hasMoreTokens()){
            // returns a string
            System.out.println(" "+strtokenizer.nextToken());
        }
         
        StringTokenizer stokenizer = new StringTokenizer(s, "-");
        System.out.println("\nThe stokenizer includes "+ stokenizer.countTokens() +" tokens.");
         
        while(stokenizer.hasMoreElements()) {
            // returns an object
            String tok = (String) stokenizer.nextElement();
             
            System.out.println("- "+tok.toString());
        }
    }
 
}

As you can see in the code above, we create two instances of StringTokenizer. In the first one we define the default delimiter, while in the second one we choose another string delimiter. In order to return all the tokens, we use a while loop where hasMoreTokens() or hasMoreElements() method is called which indicates whether there are available tokens. In addition, two operations exist ( nextToken() and nextElement()) in order to return the value of the token. The difference between them is that nextElement() method returns an Object and not a String. Finally, countTokens() function returns the number of the tokens each time.

Now you can see the output of the execution. As you can notice, there are the same tokens for both strings. This is happening because StringTokenizer ignores empty elements and also we utilized the delimiter to separate tokens in both instances.

Output

01
02
03
04
05
06
07
08
09
10
11
12
 Java
 Code
 Geeks
 Examples
 !
 
The stokenizer includes 5 tokens.
-Java
-Code
-Geeks
-Examples
-!

3. Download the source code

This was an example of stringtokenizer in Java.

Download
Download the source code of this example: Java StringTokenizer Example
0 0 vote
Article Rating

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Subscribe
Notify of
guest
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x