All About Class StringTokenizer in Java



StringTokenizer class allows us to break a string into tokens.The tokenization method is much simpler than the one used by StringTokenizer class.The StringTokenizer does not distinguish among identifier,numbers and quoted Strings.It also does not recognize and skip comments.
The set of delimiters(the characters that separate tokens) may be specified either at creation time or on a per token basis.
An instance of StringTokenizer behaves in one of the two ways,depending on whether it was created with the return tokens flag.This flag can have two values true or false.


Case-1::If the flag is false:
If the flag is false,delimiters characters merely serve to separate tokens.A token is a maximal sequence of consecutive characters that are delimiters.
Case-2::If the flag is true:
if the flag is true,delimiters characters are considered to be tokens.A token is either one delimiter character or a maximal sequence of consecutive characters that are not delimiters.

How to work:

StringTokenizer  st=new StringTokenizer("I love Java");
while(st.hasMoreToken())
{
System.out.println(st.nextToken());
}
 
The output of the code is:
I
love
Java
The structure of class StringTokenizer is given as:

public class java.util.StringTokenizer extends java.lang.Object implements java.util.Enumeration{
//constructors:
public StringTokenizer(String str);//constructs a StringTokenizer for the specified String str.The tokenizer uses the default delimiter set,which is "\t\n\r",the space character,the tab character,the newline character and the carriage return character.
public StringTokenizer(String str,String delimiter);//constructs a StringTokenizer for the specified String str.The character specified in the argument as String delimiter is the delimiter for separating tokens.
public StringTokenizer(String str,String delimiter,boolean returnTokens);//constructs a StringTokenizer for the specified String str.The character specified in the argument as String delimiter is the delimiter for separating tokens.If the returnTokens flag is true,then the delimiter characters are also returned as Tokens.Each delimiter characters are also returned as a String of length one.
If the flag is false,the delimiter characters are skipped and only serve as separators between tokens.
//Methods:
public int countTokens();
public boolean hasMoreElements();
public boolean hasMoreTokens();
public Object nextElement();
public String nextToken();
public String nextToken(String delimit)
}
 
All About Class StringTokenizer in Java All About Class StringTokenizer in Java Reviewed by Animesh Chatterjee on October 04, 2018 Rating: 5

No comments:

Powered by Blogger.