TechFundu

  • Increase font size
  • Default font size
  • Decrease font size
Welcome Guest,  Login | Join Now
Home Groups Core Java Developers Discussions Scanner vs. StringTokenizer vs. String.Split
Scanner vs. StringTokenizer vs. String.Split
Discussion started by Deepa Mehta , on 27 July 08:57 AM
I just learned about Java's Scanner class and now I'm wondering how it compares/competes with the StringTokenizer and String.Split. I know that the StringTokenizer and String.Split only work on Strings, so why would I want to use the Scanner for a String? Is Scanner just intended to be one-stop-shopping for spliting?
Replies
You will need to be a member of this group first before you can post a reply.
Pravin Jain
The StringTokenizer uses a set of charactrers as delimiters, whereas The Scanner and the String class's split method uses the regular expression as delimiter.
The Scanner is rich in teh regular expressions which it uses for testing and then getting the next token. eg. we have the hasNextInt method which can test whether the next token identified using the delimiter is a valid int or not. This kind of testing before testing is not available with the other two options.
There is another major difference between StringTokenizer and the other two options.
Say we have a String s = "abc,xyz,mno,,pqr", and we use ',' as a delimiter then the StringTokenizer would give four tokens, whereas the other two would give us five tokens, the secondlast token would be a null. The Scanner class also has advantage of working directly on a character stream.
Hope this is helpful in understanding the difference between Scanner and StringTokenizer.
Friday, 01 January 2010 22:36
 

TechFundu Polls

Which web browser do you like most?

Follow us on Twitter