Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>They're essentially horses for courses.</p> <ul> <li><code>Scanner</code> is 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.</li> <li><code>String.split()</code> and <code>Pattern.split()</code> 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.</li> <li><code>StringTokenizer</code> is even more restrictive than <code>String.split()</code>, 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 as <code>String.split()</code>. (See my <a href="http://www.javamex.com/tutorials/regular_expressions/splitting_tokenisation_performance.shtml" rel="noreferrer">comparison of <code>String.split()</code> and <code>StringTokenizer</code></a>.) It also predates the regular expressions API, of which <code>String.split()</code> is a part.</li> </ul> <p>You'll note from my timings that <code>String.split()</code> can still tokenize <strong>thousands of strings in a few milliseconds</strong> on a typical machine. In addition, it has the advantage over <code>StringTokenizer</code> that it gives you the output as a string array, which is usually what you want. Using an <code>Enumeration</code>, as provided by <code>StringTokenizer</code>, is too "syntactically fussy" most of the time. From this point of view, <code>StringTokenizer</code> is a bit of a waste of space nowadays, and you may as well just use <code>String.split()</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload