Home » Core Java » util » StringTokenizer » Tokenize String with StringTokenizer

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Tokenize String with StringTokenizer

In this example we shall show you how to tokenize a String with StringTokenizer. The StringTokenizer is used to break a String into tokens. To tokenize a String with StringTokenizer one should perform the following steps:

  • Get a new StringTokenizer for a specified String, using the StringTokenizer(String str) constructor.
  • Get the tokens of the String, using hasMoreTokens() and nextToken() API methods of StringTokenizer. Since no delimiter has been set, one of the delimiters in the default delimiter set is used, that is here the space character,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.StringTokenizer;

public class TokenizeStringWithStringTokenizer {
	
	public static void main(String[] args) {
		
		StringTokenizer st = new StringTokenizer("Java Code Geeks - Java Examples");
		 
		// lop through tokens
		while(st.hasMoreTokens()) {
			System.out.println(st.nextToken());
		}
		
	}

}

Output:

Java
Code
Geeks
-
Java
Examples

 
This was an example of how to tokenize a String with StringTokenizer in Java.

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