Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note that the regex need not be recompiled each time. From the <a href="http://download-llnw.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29" rel="noreferrer">Javadoc</a>:</p> <blockquote> <p>An invocation of this method of the form <code>str.split(regex, n)</code> yields the same result as the expression</p> </blockquote> <pre><code>Pattern.compile(regex).split(str, n) </code></pre> <p>That is, if you are worried about performance, you may precompile the pattern and then reuse it:</p> <pre><code>Pattern p = Pattern.compile(regex); ... String[] tokens1 = p.split(str1); String[] tokens2 = p.split(str2); ... </code></pre> <p>instead of </p> <pre><code>String[] tokens1 = str1.split(regex); String[] tokens2 = str2.split(regex); ... </code></pre> <p>I believe that the main reason for this API design is convenience. Since regular expressions include all "fixed" strings/chars too, it simplifies the API to have one method instead of several. And if someone is worried about performance, the regex can still be precompiled as shown above.</p> <p>My feeling (which I can't back with any statistical evidence) is that most of the cases <code>String.split()</code> is used in a context where performance is not an issue. E.g. it is a one-off action, or the performance difference is negligible compared to other factors. IMO rare are the cases where you split strings using the same regex thousands of times in a tight loop, where performance optimization indeed makes sense.</p> <p>It would be interesting to see a performance comparison of a regex matcher implementation with fixed strings/chars compared to that of a matcher specialized to these. The difference might not be big enough to justify the separate implementation.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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