• Entries (RSS)
  • Comments (RSS)

StringTokenizer replacement

Posted by | Posted in Java | Posted on 07-10-2007

Tagged Under :

The StringTokenizer class in java.util package has been deprecated as of Java 1.4. Sun recommends the use of String.split method instead of java.util.StringTokenizer class.

For eg:

StringTokenizer strTokens = new StringTokenizer("Token String");
 
   while (strTokens.hasMoreTokens())  {
             System.out.println(strTokens.nextToken());
   }

Can be replaces with the following code.

    String[] splittedSting = "Token Sting".split("\\s");
   for (int x=0; x<splittedSting.length; x++) {
       System.out.println(splittedSting[x]);
    }

Share

Read More

Comments

1 comments posted onStringTokenizer replacement

  1. Could it be a good idea to replace fast and optimized code “StringTokenizer” with a 3 times slower String.split() ? String.split is using Pattern and a Matcher from regex. From this point of view you trade ashes for gold..

Post a Comment