Monday, February 2, 2009

StringTokenizer tutorial - StringTokenizer example



The StringTokenizer class provides a means of converting text strings into
individual tokens. By specifying a set of delimiters, you can parse text strings
into tokens using the StringTokenizer class. String tokenization is useful in a
wide variety of programs, from compilers to text-based adventure games.

public class StringTokenizer extends Object implements Enumeration

The string tokenizer class allows an application to break a string into tokens.
The tokenization method is much simpler than the one used by the
StreamTokenizer class. The StringTokenizer methods do not distinguish
identifiers,numbers,and quoted strings,nor do they 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 two ways,depending on whether
it was created with the returnTokens flag having the value true or false:

• If the flag is false, delimiter characters serve to separate tokens.
A token is a maximal sequence of consecutive characters that are not delimiters.
• If the flag is true, delimiter characters are considered to be tokens.
A token is either one delimiter character, or a maximal sequence of consecutive
characters that are not delimiters.

methods and constructors:
1. To create an object to StringTokenizer
StringTokenizer st=new StringTokenizer(string); // here default delimeter space.
StringTokenizer st=new StringTokenizer(string,delimeter);
StringTokenizer st=new StringTokenizer(string,delimeter,boolean returnDelims);

2.To find next piece in the string
String token=st.nextToken();

3.To know if more tokens are remaining.
boolean x=st.hasMoreTokens();

4.To know how many number of pieces are there .
int no=st.countTokens();

StringTokenizer Example:
package collection;

import java.util.StringTokenizer;

public class StringTokenizerDemo {
static String str="My name is ravi."+ "I am working in Javapoint as a Faculty.";

public static void main(String[] args) throws Exception
{
StringTokenizer st=new StringTokenizer(str," ");

int i=st.countTokens();
System.out.println("count="+i);
while(st.hasMoreTokens())
{
String val=st.nextToken();
System.out.println(" "+val);
}

}
}


output:
count=11
My
name
is
ravi.I
am
working
in
Javapoint
as
a
Faculty.


Example 1:
StringTokenizer st = new StringTokenizer("this is java");
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
output:
this
is
java

Example 2:
StringTokenizer st = new StringTokenizer("java@sun","@");
System.out.println(st.countTokens());
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
output:
2
java
sun

Example 3:
StringTokenizer st = new StringTokenizer("java@sun","@",true);
System.out.println(st.countTokens());
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());

output:
3
java
@
sun

0 comments:

Post a Comment