-1

I found the following behavior of String split quite unpleasant:

String[] parts = "0..".split("\\.");

This would result in parts containing only "0"; however if the input is changed from "0.." to "..0"

String[] parts = "..0".split("\\.");

This would result in parts containing "", "", and "0". Can someone help explain why split behaves in this way? How was this method implemented?

Yinfang Zhuang
  • 443
  • 1
  • 5
  • 15
  • 1
    **Read the documentation**, i.e. the javadoc of [`split(String regex)`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-), which refers to [`split(String regex, int limit)`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#split-java.lang.String-int-) for full explanation. – Andreas Dec 23 '19 at 05:48
  • The bigger question is why the first dot in `.0.` (which does generate an empty match) is treated differently than the last dot (which does not). But, you didn't actually ask that question. – Tim Biegeleisen Dec 23 '19 at 05:50
  • @TimBiegeleisen Why is that the bigger question? A leading `.`, a trailing `.`, and any consecutive `.`'s cause empty values, and trailing empty values are eliminated. It's really quite that simple. `split("..A.B..C..", -1)` becomes `["", "", "A", "B", "", "C", "", ""]`, so `split("..A.B..C..")` becomes `["", "", "A", "B", "", "C"]`. – Andreas Dec 23 '19 at 05:52
  • 1
    Does this answer your question? [Java String split removed empty values](https://stackoverflow.com/questions/14602062/java-string-split-removed-empty-values) – Matt U Dec 23 '19 at 06:04
  • Could you please edit you questions to include inputs with their actual outputs? – NomadMaker Dec 23 '19 at 06:31

1 Answers1

1

Quoting from Java docs: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)

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.

This means that the empty strings are only included when there is at least one non-empty string that succeeds them.

import java.util.Arrays;
public class StringQ{

    public static void main(String args[]){
        String first = "0..";
        System.out.println(Arrays.asList(first.split("\\.")));
        // [0]
        String second = "..0";
        System.out.println(Arrays.asList(second.split("\\.")));
        // [, , 0]
        String third = "...0..0...";
        System.out.println(Arrays.asList(third.split("\\.")));
        // [, , , 0, , 0]
    }
}