StringTokenizer replacement

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]);
    }
Did you like this? If so, please
tell a friend
about it, and subscribe to the blog RSS feed.

Share/Save/Bookmark

If you enjoyed this post, make sure you subscribe to my RSS feed!



Related Posts:
  • Recursively delete a directory
  • Sending an e-mail using WebSphere Mail session settings.
  • Sorting an ArrayList of objects.
  • LIKE clause with PreparedStatement
  • Exception stackTrace to a String Variable


  • One Response to “StringTokenizer replacement”  

    1. 1 believeIT

      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..

    Leave a Reply