Skip to content

String Tokenizer

The java.util.StringTokenizer class allows you to break a string into tokens. It is simple way to break string.

The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.

import java.util.StringTokenizer;
public class Simple{
public static void main(String args[]){
StringTokenizer st = new StringTokenizer("apple ball cat dog"," ");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}
}

Output:

apple

ball

cat

dog

public static void main(String args[]) {
StringTokenizer st = new StringTokenizer("apple,ball cat,dog", ",");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}
}

Output:

apple

ball cat

dog