I frequent the Java online discussion forums to keep my programming skills sharp. Recently, I learned something that surprised me. I responded to another user and suggested that he use Java’s StringTokenizer class. A senior member quickly corrected me by stating that future use of the StringTokenizer class was discouraged. Sure enough, I found this quote in the Java API: “StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.“ Well, that was a surprise to me. I had no idea that StringTokenizer was being phased out. But it does make sense. Anyone who has used StringTokenizer knows how cumbersome and confusing it can be to work with.
StringTokenizer was a very useful class. If you have not used it before, it can take a string and split it into multiple strings based on a delimiter. So if your string is “RED WHITE BLUE” and the delimiter is a single space ” “, then it would split the string into “RED”, “WHITE”, and “BLUE”. You can imagine how useful that can be when processing text files, strings, etc. So how are we going to accomplish this without StringTokenizer? The answer is Regular Expressions.
In Java, regular expressions can be used to declare a Pattern. It is similar to regular expressions in the Perl language. There is a little bit of a learning curve with regular expressions. At first, they seem cryptic. But then you realize just how powerful they are. Regular expressions are a very complete way to describe detailed patterns. Once you know how to define a regular expression, you can use that expression to define a Pattern that can split a string (just like StringTokenizer). Since you can come up with creative ways to describe patterns, you now have creative ways to split up a string.
Once you define a pattern with a regular expression, you can split a String object. This duplicates the behavior of the StringTokenizer class. However, it is a much cleaner and simpler approach. Here is an example:
String sSentence = “RED WHITE BLUE”;
Pattern p = Pattern.compile(“\s”); //pattern that indicates a space character
String result = p.split(sSentence);
for (int i=0; i
Here is the output:
For those of you who have used StringTokenizer before, you can see what a breath of fresh air this is. It is very simple and straightforward. After finally embracing regular expressions, I understand why Java has decided to phase out StringTokenizer. I am happy to say that I will never use StringTokenizer again. Regular expressions are the way to go. Give it a try!
You can learn more about Regular Expressions here: Lesson: Regular Expressions.
1. Java 2 Platform Standard Ed. 5.0, “java.util Class StringTokenizer”, http://download.oracle.com/javase/1.5.0/docs/api/java/util/StringTokenizer.html
2. The Java Tutorials, “Lesson: Regular Expressions”, http://download.oracle.com/javase/tutorial/essential/regex/intro.html