Java StringTokenizer example
Published: January 17, 2010 , Updated: January 11, 2010 , Author: mkyong
The StringTokennizer class is use to split a String object into different tokens by certain delimiter. (space is the default delimiter)
StringTokenizer example
Here’s an example to split a string by “space” and “comma” delimiter, and iterate the StringTokenizer elements and print it out one by one.
package com.mkyong.common; import java.util.StringTokenizer; public class App { public static void main( String[] args ) { String str = "This is String , split by StringTokenizer, created by mkyong"; StringTokenizer st = new StringTokenizer(str); System.out.println("---- Split by space ------"); while(st.hasMoreElements()){ System.out.println(st.nextElement()); } System.out.println("---- Split by comma ',' ------"); StringTokenizer st2 = new StringTokenizer(str,","); while(st2.hasMoreElements()){ System.out.println(st2.nextElement()); } } }
Output
---- Split by space ------ This is String , split by StringTokenizer, created by mkyong ---- Split by comma ',' ------ This is String split by StringTokenizer created by mkyong
Reference
http://java.sun.com/javase/6/docs/api/java/util/StringTokenizer.html
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
thank you very much, for your great function!
I’m new in the world of java, but it is like php
It’s useful, but it is suggested to better use split method, reference http://download.oracle.com/javase/1.5.0/docs/api/java/util/StringTokenizer.html
It is very useful for me.
Thank u for giving this information.
Your blog is very useful for me. Thanks!