Why does "||".split("\\|").length return 0 and not 3?

Viewed 2253

When there are adjacent separators in the split expression I expect null or an empty string--not have it eliminated.

The Java code is below:

public class splitter {
    public static void main(String args[]) {
        int size = "||".split("\\|").length;
        assert size == 3 : "size should be 3 and not " + size;
    }
}

I expected to get either { "", "", "" } or { null, null, null }. Either would be fine.

Perhaps there's a regular expression that will not be fooled by empty words?

2 Answers
Related