Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Consider the split expression <code>",1,2,3,4".split(",");</code></p> <p>What would you expect? Right, an empty-string to start with. In your case you have a 'nothing' in front of the first 'a' as well as one behind it.</p> <p>Update: comments indicate this explanation is not enough of an explanation (which it may not be)... but, it really is this simple: the engine starts at the beginning of the string, and it looks to see if what's in front of it matches the pattern. If it does, it assigns what's behind it to a new item in the split.</p> <p>On the first character, it has "" (nothing behind it), and it looks to see if there's "" (the pattern) in front of it. There is, so it creates a "" match.</p> <p>It then moves on, and it has 'a' behind it, and again, it again has "" in front of it. So the second result is an "a" string.</p> <p>An interesting observation is that, if you use <code>split("", -1)</code> you will also get an empty-string result in the last position of the result array.</p> <hr> <p>Edit 2: If I wrack my brains further, and consider this to be an academic exercise (I would not recommend this in real life...) I can think of only one good way to do a regex <code>split()</code> of a String into a <code>String[]</code> array with 1 character in each string (as opposed to char[] - which other people have given great answers for....).</p> <pre><code>String[] chars = str.split("(?&lt;=.)", str.length()); </code></pre> <p>This will look behind each character, in a non-capturing group, and split on that, and then limit the array size to the number of characters (you can leave the <code>str.length()</code> out, but if you put <code>-1</code> you will get an extra space at the end)</p> <p>Borrowing nitro2k01's alternative (below in the comments) which references the string beginning and end, you can split reliably on:</p> <pre><code>String[] chars = str.split("(?!(^|$))"); </code></pre>
    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. 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