1
1 answers
1
Tokenizer class in Java is used to break a string into tokens so they can be accessed one at a time. Have a look at the following code segment on its usage:
String str="This is a test";
StringTokenizer st = new StringTokenizer(str);
//As long as there are tokens available
while(st.hasMoreTokens()){
//Convert the StringTokenizer to String
String s = st.nextToken().toString();
System.out.println(s);
}
This code will print the words stored in string str on different lines. What StringTokenizer has done here is, it has broken the string in tokens based on the default delimiter i.e. blank space, and they are accessed using nextToken() method.
Hope that makes it clear to you now.
Log in to your account to answer this questions.