1

I don't understand why this is getting an output of 3:

String[] split = "name:john;;sex:m;;".split(";");
System.out.println(Arrays.toString(split) + " size " + split.length);

I have read Oracle documentation and I still wihout getting why is 3.

Why the output is:

[name:john, , sex:m] size 3

Where is taking the ' ' (the second on the list), and also why the ";;" at the end is not in the output.

2
  • 1
    Nothing is. You split on ;, so that emptiness between the ;; is whats taking it. Split on ;; Commented Oct 25, 2015 at 19:05
  • 2
    And from the Javadoc Trailing empty strings are therefore not included in the resulting array. Commented Oct 25, 2015 at 19:07

4 Answers 4

3

Documentation says

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.

here

Sign up to request clarification or add additional context in comments.

Comments

1

split(";") will get strings from before, between, and after each single semicolon. So your split array would theoretically be {"name:john","","sex:m","",""} at some point in the operation. However, split() removes trailing empty strings, so you actually get {"name:john","","sex:m"}.

As to why you don't have a terminal semicolons being printed, is because you split over each individual semicolon without a limit. The limit is length of the resulting array.

You can often fix internal empty strings by using the proper regex for multiple characters. You may want to check split(";++"). If you want the terminal semicolons you need to set a limit split(";++",2).

1 Comment

@DanielHernández The documentation for docs.oracle.com/javase/8/docs/api/java/lang/… should explain it, and the link their describes all regular expressions.
0

It looks like Arrays.toString is representing your split array with space characters, instead of what it would actually be in the array of Strings returned by String.split, which is the empty string "". The final two semicolons on the end are not in the output because String.split only inserts the empty strings into its array where the delimiter you specify occurs within the string, not after it ends.

2 Comments

Yes, it is , I have printed those in for-each and it is empty ! nice eye
It looks like ... but "The final two semicolons on the end are not in the output because String.split only inserts the empty strings into its array where the delimiter you specify occurs within the string, not after it ends." That is the part I dont from the Oracle documentation
0

The '' is from the two semicolons that you put in between name:john and sex:m. The fact that they are not at the end is because java ignores empty strings at the end of a list.

3 Comments

Java doesn't ignore anything, split() does and in a surprising move there's a difference between Java 7 and Java 8 in its functionality.
Just those at the end of the list?
Yes, only the ones at the end. the ones in the middle are kept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.