StringTokenizer

 

The snippet shows how to use StringTokenizer to count the words in a file while changing the token delimiter to a dollar sign. The default delimiter is a blank. i.e.

 

String s = "Mary$had$a$little$lamb.";

StringTokenizer st = new StringTokenizer(s, "$"); // $ is now the delimiter. Remove the second $ parm to use blanks.

System.out.println("There are " + st.countTokens( ) + " tokens present");

int count = 1;

while (st.hasMoreTokens( )) {

String t = st.nextToken();

System.out.println("Token " + count + " is " + t);

count ++;

}