0

In java doc of public String\[\] split(String regex)

we can find example:

The string "boo:and:foo", for example, yields the following results with these expressions

Regex          Result

o                   { "b", "", ":and:f" }

What is the second result "" come from? From between b and oo, or oo and :and:f?

Community
  • 1
  • 1
user1169587
  • 1,104
  • 2
  • 17
  • 34
  • this return two string, 'b', ':and:f', but why the origin '' not found? I know '+' means the 'o' must occur at least one time – user1169587 Aug 24 '16 at 09:55
  • @DavidPérezCabrera if use split("o+", -1), then three string return, 'b', ':and:f' and '', don't know what happen....why the last '' is counted but the '' between 'o''o' is not counted? – user1169587 Aug 24 '16 at 10:10

2 Answers2

4

When you use the delimiter o and there are two delimiters next to eachother, it yields an empty string. So for instance if you have a String foo::bar and split it with : you will get {"foo", "", "bar"} because the space between :: is treated as an empty string when splitting.

If you would like to split without having any empty strings, take a look at this question.

Ending empty strings?

When split is given zero or no limit argument it discards trailing empty fields

Ref: Java: String split(): I want it to include the empty strings at the end

Community
  • 1
  • 1
px06
  • 2,256
  • 1
  • 27
  • 47
  • 1
    what about last oo then? . No empty string token in the end. – omkar sirra Aug 24 '16 at 09:15
  • 2
    @omkarsirra Yes, the empty strings at the end fall off (take a look at this for some in depth discussion about it http://stackoverflow.com/questions/13939675/java-string-split-i-want-it-to-include-the-empty-strings-at-the-end – px06 Aug 24 '16 at 09:16
1

Note that for a regex engine, locations between characters also count. So, the regex engine sees the boo:and:foo string as

<EMPTY>b<EMPTY>o<EMPTY>o<EMPTY>:<EMPTY>a<EMPTY>n<EMPTY>d<EMPTY>:<EMPTY>f<EMPTY>o<EMPTY>o<EMPTY>
               ^between^                                                       ^between^

As the first ^between^ is not at the end of the string, split keeps the empty location (string) in the results. The trailing empty elements are removed as the end since the int limit argument is 0 by default. If you pass -1 (a non-positive value), all the trailing empty elements will be present in the resulting array. See String#split reference.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563