Thursday, 5 April 2012

Class - StringToKenizer


StringToKenizer

The processing of text consist of parsing a formatted. input string, which is the division of text into a set of discrete parts,or tokens.

String Tokenizer, specify an input string  and a string that contains delimeters. Delimeters are characters that separate tokens. StringTokenizer implements the enumeration interface.

Constructors:

  StringTokenizer(String str)
  StringTokenizer(String str,string delimeters)
StringTokenizer(String str,string delimeters,boolean delimAsToken)

In all constructor, str is the string that will be tokenized.

In first version, the default delimeters are used.

In second & third version delimeters is a string that specifies the delimeters.

In third version delimAsToken is true, then the delimeters are also returned as tokens when the string is pursed. Otherwise, the delimeters are not returned as token by the first two forms.

Methods:

int countTokens()
Using the current set of delimeters the method delemeters, the method determines the number of tokens left to be parsed and returns the result.

boolean hasMoreElements()
Returns true if one or more tokens remain in the string and returns false if there are none.

boolean hasMoreTokens()
Returns true if one or more tokens remain in the string and returns false if there are none.

object nextElement()
            Returns the next token as an object.

string nextToken()
            Returns the next token as a string.

string nextToken(string delimeters)
Returns the next token as a string and sets the delimeters string to that specified by delimeters.


import java.util.*;
class StringTokenDemo 
{
    public static void main(String args[]) 
    {
  
String str = "Name = Ruchita;" +
"Age=24;" +
"Educational Qualification = Msc(IT);" +
"Hobby = Reading,Dancing,Writing";

StringTokenizer st = new StringTokenizer(str, "=;");

while(st.hasMoreTokens()) 
{
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key+ "----->" + val);
}
    }
}








Posted By  : Ruchita Pandya

4 comments:

  1. hi ruchita i like ur documents n very helpful to me thanks a lot................

    ReplyDelete
  2. Nice to read this blog its very interesting to read and more valuable point in your blog .Thanks for your informative article.
    java training in chennai |
    java training institutes in chennai

    ReplyDelete
  3. Learned a lot of new things from your post ,It's amazing blog


    Core Java Online Training

    ReplyDelete