KickJava   Java API By Example, From Geeks To Geeks.

Java > Java SE, EE, ME > java > util > StringTokenizer

java.util
Class StringTokenizer

java.lang.Object
  extended by java.util.StringTokenizer
All Implemented Interfaces:
Enumeration<Object>
See Also:
Top Examples, Source Code, StreamTokenizer

public int countTokens()
See Also:
nextToken()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1294]Parse string
By Anonymous on 2005/02/04 22:25:05  Rate
import java.util.*; 
  
  
 class ParseString 
    {   
   public static void main  ( String args [  ]  )   
    {  
   String s = "first,second, third fourth, fifth"; 
   StringTokenizer st; 
  
  
   st = new StringTokenizer  ( s, ", " ) ; 
  
  
   while  ( st.hasMoreTokens  (  )  )  
      {  
     System.out.println  ( " >  > " + st.nextToken  (  )  + " <  < " ) ; 
      }  
    }   
    }  
    
    
 The output is: 
  
  
    >  > first <  <  
    >  > second <  <  
    >  > third <  <  
    >  > fourth <  <  
    >  > fifth <  < 


[1531]Empty tokens are not counted
By wprecht { at } umuc { dot } edu on 2005/09/09 07:45:28  Rate
It should be noted that empty tokens are not counted.   
 Taking the above code snippet for examples, if you 
 had an input string: "first,, second, third", you  
 get 3 tokens. Also empty tokens are skipped with  
 nextToken (  ) .


public boolean hasMoreElements()
See Also:
hasMoreTokens(), Enumeration, Object
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public boolean hasMoreTokens()
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[15]Replace XML Chars
By Anonymous on 2005/05/06 22:18:24  Rate
public static String replaceXMLChars ( String srcStr )   {  
   StringTokenizer tok = null; 
   StringBuffer result = new StringBuffer (  ) ; 
   String part = null; 
  
  
   tok = new StringTokenizer ( srcStr, " <  > &'\"", true ) ; 
   while  ( tok.hasMoreTokens (  )  )   {  
     part = tok.nextToken (  ) ; 
     if  ( part.endsWith ( " < " )  )   {  
       result.append ( part.substring ( 0, part.length (  )  - 1 )  + "<" ) ; 
      }  
     else if  ( part.endsWith ( " > " )  )   {  
       result.append ( part.substring ( 0, part.length (  )  - 1 )  + ">" ) ; 
      }  
     else if  ( part.endsWith ( "&" )  )   {  
       result.append ( part.substring ( 0, part.length (  )  - 1 )  + "&" ) ; 
      }  
     else if  ( part.endsWith ( "'" )  )   {  
       result.append ( part.substring ( 0, part.length (  )  - 1 )  + "'" ) ; 
      }  
     else if  ( part.endsWith ( "\"" )  )   {  
       result.append ( part.substring ( 0, part.length (  )  - 1 )  + """ ) ; 
      }  
     else  {  
       result.append ( part ) ; 
      }  
    }  
   return result.toString (  ) ; 
  }  
 


public Object nextElement()
See Also:
nextToken(), Enumeration, NoSuchElementException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String nextToken()
See Also:
NoSuchElementException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public String nextToken(String delim)
See Also:
NullPointerException, NoSuchElementException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1666]Use Split instead of StringTokenizer
By Anonymous on 2005/11/04 20:23:08  Rate
StringTokenizer is a legacy class. We should use the split method of String or the java.util.regex package instead  
  
  
 StringTokenizer st = new StringTokenizer ( "this is a test" ) ; 
   while  ( st.hasMoreTokens (  )  )   {  
        System.out.println ( st.nextToken (  )  ) ; 
  }  
   
 Can be rewritten to  
  
  
 String [  ]  result = "this is a test".split ( "\\s" ) ; 
 for  ( int x=0; x < result.length; x++ )  
     System.out.println ( result [ x ]  ) ; 
  
  
  
 both print the outputs 
  
  
      this 
      is 
      a 
      test


public StringTokenizer(String str)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


public StringTokenizer(String str,
                       String delim)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1463]strTokAdd
By Reid_Judd on 2005/06/24 20:42:57  Rate
/** strTokAdd.java **/ 
  
  
 import  java.io.*; 
 import java.util.*; 
  
  
 public class strTokAdd  {  
  
  
   static public void main ( String args [  ]  )   {  
     int total = 0; 
     String ones = "1, 11, 111, 1111, 11111, 111111, 1111111"; 
  
  
     /// remove spaces and commas in the string 
     StringTokenizer strtok = new StringTokenizer ( ones, ", " ) ; 
  
  
     while  ( strtok.hasMoreTokens (  )  )   {  
       total += Integer.parseInt (  strtok.nextToken (  )   ) ; 
      }  
     System.out.println ( "sum of " + ones + " = " + total ) ; 
     System.exit ( 0 ) ; 
    }  
  }  
 /** Outputs: 
  * sum of 1, 11, 111, 1111, 11111, 111111, 1111111 = 1234567 
  */


public StringTokenizer(String str,
                       String delim,
                       boolean returnDelims)
See Also:
NullPointerException
Geek's Notes:
Description  Add your codes or notes  Search More Java Examples  


[1420]Use the StringTokenizer constructor with the useDelim
By Anonymous on 2005/05/10 07:15:13  Rate
Avoid absolutely. 
 Either you use the constructor with the useDelim and then if you parse 
 A,,B 
 you get "A" "," "," "B" 
 Or you don't use it and you get 
 "A" and "B". 
 When what you want is "A" "" and "B". 
  
  
 Use String.split instead. 
  
  
 

Popular Tags