Thursday, April 3, 2014

StringTokenizer


StringTokenizer is class can be utilize to splitting the string into tokens. If no delimiter is mentioned then Whitespace will be taken as default delimiter but we can use multiple delimiter in StringTokenizer to split the tokens.

Methods

int countTokens() – Returns number of tokens in the String.
boolean hasMoreTokens() – Returns true if more tokens exists otherwise returns false.
String nextToken() - Returns the next token from the given the String.  
String nextToken(String delimiter) - returns the next token while switching to the new given delimiter.
boolean hasMoreElements() - Returns same as hasMoreTokens method.
Object nextElement()  - Returns same as nextToken but return value is an Object rather than String.

Example-1 : countTokens() / hasMoreTokens() / nextToken().


import java.util.*;

    public class StrTok_1
        {
            public static void main(String[] args)
                {
                    StringTokenizer st = new StringTokenizer("the brown dog jumped over the lazy fox");
                    System.out.println("\n\tTotal Numbers of tokens : " + st.countTokens());
                    System.out.println("\tTokens : " );
                    while (st.hasMoreTokens())
                        {
                            System.out.println("\t"+st.nextToken());   
                        }
     
                }
       }


Output



          Total Numbers of tokens : 8
          Tokens :
          the
          brown
          dog
          jumped
          over
          the
          lazy
          fox


Example-2 : hasMoreElements() / nextElement()

import java.util.*;

    public class StrTok_2
        {
            public static void main(String[] args)
                {
                    StringTokenizer st = new StringTokenizer("the brown dog jumped over the lazy fox");
                    System.out.println("\tTokens : " );
                    while (st.hasMoreElements())
                        {
                            System.out.println("\t"+st.nextElement());   
                        }
                  }
       }


Output



          Total Numbers of tokens : 8
          Tokens :
          the
          brown
          dog
          jumped
          over
          the
          lazy
          fox


Example_3 : nextToken(String Delimiter)
import java.util.*;

    public class StrTok_3
        {
            public static void main(String[] args)
                {
                    StringTokenizer st = new StringTokenizer("you,too,Brutus");
                    System.out.println("\tTokens (Delimited with , ) : " );
                    while (st.hasMoreTokens())
                        {
                            System.out.println("\t"+ st.nextToken(","));;   
                        }
                  }
       }


Output



          Tokens (Delimited with , ) :
          you
          too
          Brutus



Example_4 : Multiple Delimiter

import java.util.*;

    public class StrTok_4
        {
            public static void main(String[] args)
                {
                    StringTokenizer st = new StringTokenizer("Hi! I am fine. How are you?",".!?");
                    System.out.println("\n\tTotal Numbers of tokens : " + st.countTokens());
                    System.out.println("\tTokens (Delimiter . ! ? ): " );
                    while (st.hasMoreTokens())
                        {
                            System.out.println("\t"+st.nextToken().trim());   
                        }
                  }
       }



Output



          Total Numbers of tokens : 3
          Tokens (Delimiter . ! ? ):
          Hi
          I am fine
          How are you




No comments: