Why "||" .split ("\\ |"). Does length return 0, not 3?

When there are adjacent delimiters in the delimiter expression, I expect null or an empty string to not eliminate it.

The following is the Java code:

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}. Or everything will be fine.

Perhaps there is a regular expression that will not be deceived by an empty word?

+7
source share
2 answers

According to javadoc :

This method works as if it were calling a method with two split arguments with a given expression and a limit argument of zero. Thus, trailing blank lines are not included in the resulting array.

javadoc for split(String, int) clarifies:

The limit parameter controls the number of uses of the template and, therefore, affects the length of the resulting array. If the limit n is greater than zero, the pattern will be applied no more than n - 1 times, the length of the array will be no more than n, and the last element of the array will contain all input data outside the last matched separator. If n is not positive, the pattern will be applied as many times as possible, and the array can be of any length. If n is zero, the pattern will be applied as many times as possible, the array can be of any length, and the final empty lines will be discarded.

(emphasis mine)

So, to return an array of empty lines, call "||".split("\\|", -1)

+14
source

I need to take a closer look at Paul's answer (his view is simpler), but I was able to find something in open source expressions that allow statements (I apologize that the code is in Apex - it just transfers Java).

 static testMethod void testPatternStringSplit() { Pattern aPattern = Pattern.Compile('(?=\\|)'); system.assertEquals(3, aPattern.split('||').size()); system.assertEquals(3, aPattern.split(' | | ').size()); system.assertEquals(3, aPattern.split('a|b|c').size()); system.assertEquals(3, aPattern.split('a|b|').size()); system.assertEquals(3, aPattern.split('|b|c').size()); system.assertEquals(3, aPattern.split('|b|').size()); } 

I need to write code to test Paul ...

0
source

All Articles