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?
According to the javadoc:
The javadoc for
split(String, int)elaborates:(emphasis mine)
So to return an array of empty strings, call
"||".split("\\|", -1)