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?
I just learned about Java’s Scanner class and now I’m wondering how it compares/competes
Share
They’re essentially horses for courses.
Scanneris designed for cases where you need to parse a string, pulling out data of different types. It’s very flexible, but arguably doesn’t give you the simplest API for simply getting an array of strings delimited by a particular expression.String.split()andPattern.split()give you an easy syntax for doing the latter, but that’s essentially all that they do. If you want to parse the resulting strings, or change the delimiter halfway through depending on a particular token, they won’t help you with that.StringTokenizeris even more restrictive thanString.split(), and also a bit fiddlier to use. It is essentially designed for pulling out tokens delimited by fixed substrings. Because of this restriction, it’s about twice as fast asString.split(). (See my comparison ofString.split()andStringTokenizer.) It also predates the regular expressions API, of whichString.split()is a part.You’ll note from my timings that
String.split()can still tokenize thousands of strings in a few milliseconds on a typical machine. In addition, it has the advantage overStringTokenizerthat it gives you the output as a string array, which is usually what you want. Using anEnumeration, as provided byStringTokenizer, is too ‘syntactically fussy’ most of the time. From this point of view,StringTokenizeris a bit of a waste of space nowadays, and you may as well just useString.split().