How to Separate Words Using StringTokenizer in Java


Through this example, we will learn how to split words using StringTokenizer  in Java.

Source Code

package com.beginner.examples;

import java.util.StringTokenizer;

public class SplitWordUseStringTokenizer {

	
	public static void main(String[] args) {
		
		String words = "one,two,three,four,five,six";
		
		//Create the StringTokenizer object
		StringTokenizer tokenizer = new StringTokenizer(words,",");
		
		//iteration
		while(tokenizer.hasMoreTokens())
		{
			String word = tokenizer.nextToken();
			System.out.println(word);
		}
	}

}

Output:

one
two
three
four
five
six

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments