2

If I split "hello|" and "|hello" with "|" character, then I get one value for the first and two values for the second version.

String[] arr1 = new String("hello|").split("\\|");
String[] arr2 = new String("|hello").split("\\|");
System.out.println("arr1 length: " + arr1.length + "\narr2 length: " + arr2.length);

This prints out:

arr1 length: 1
arr2 length: 2

Why is this?

akash
  • 22,664
  • 11
  • 59
  • 87
Lahiru Chandima
  • 22,324
  • 22
  • 103
  • 179
  • 1
    `split(String regex)` - *This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.* – Aleksandr M Jul 20 '15 at 12:40
  • Check this http://stackoverflow.com/questions/15113272/java-split-by-a-successive-character/15113300#15113300 – m0skit0 Jul 20 '15 at 12:44
  • also check [this](http://stackoverflow.com/a/19503404/3841803) – singhakash Jul 20 '15 at 12:46

4 Answers4

8

According to java docs. split creates an empty String if the first character is the separator, but doesn't create an empty String (or empty Strings) if the last character (or consecutive characters) is the separator. You will get the same behavior regardless of the separator you use.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • 1
    "*You will get the same behavior regardless of the separator you use*" that depends on Java version. In pre 8 when we used `split("")` we always end up with empty string at start, but now (in Java 8) we don't (same with other zero-length regexes). – Pshemo Jul 20 '15 at 12:49
  • @Pshemo Interesting. I didn't know that. Thanks. – Eran Jul 20 '15 at 12:51
  • More info here https://stackoverflow.com/questions/22718744/why-does-split-in-java-8-sometimes-remove-empty-strings-at-start-of-result-array – Pshemo Jul 20 '15 at 12:51
3

Trailing empty String will not be included in array check the following statement.

String#split This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

akash
  • 22,664
  • 11
  • 59
  • 87
1

String#split always returns the array of strings computed by splitting this string around matches of the given regular expression.

Freak
  • 6,786
  • 5
  • 36
  • 54
1

Check the source code for the answer: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/regex/Pattern.java#Pattern.compile%28java.lang.String%29

The last lines contains the answer:

int resultSize = matchList.size();
if (limit == 0)
  while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
    resultSize--;
String[] result = new String[resultSize];

So the end will not be included if it is empty.

JFPicard
  • 5,029
  • 3
  • 19
  • 43