Java StringTokenizer Simple Example


Another very useful method is the stringtokenizer method, this method takes a standards string and breaks it into tokens(words), can be useful for  string manipulation.

---------------------CODE-----------------

import java.util.StringTokenizer;

//**EXAMPLE TO SHOW STRING TOKENIZER IN ACTION


class stringTok
{
public static void main(String args[])
{
///DEFINE A STRING TOKEN - CAN ALSO PASS STRING INTO THE STRING TOKEN 
StringTokenizer st = new StringTokenizer("A long list of text to tokenize");

//DISPLAY NUMBER OF WORDS IN THE STRING TOKEN
System.out.println("NUMBER OF WORDS : "+st.countTokens()+"\n\n");


//CYCLE THOUGH THE TOKENS AND DISPLAY EACH TOKEN(WORD) ONE BY ONE
while(st.hasMoreTokens())
System.out.printf("%s\n",st.nextToken());
}
}

------------------ OUTPUT-------------------


NUMBER OF WORDS : 7


A
long
list
of
text
to
tokenize

No comments:

Post a Comment