",".split(",")
#=> [] # really? the input string has two empty substrings delimited by a ,
",".partition(",")
#=> ["", ",", ""] # partition() handling of empty substrings makes sense
", ".split(",")
#=> ["", " "] # this makes sense to me, 2 substrings, one empty one not
" ,".split(",")
#=> [" "] # this confuses me - still 2 substrings, one empty one not
I would expect ",".split(",")
to return ["",""] but not []. And the inconsistent handling between splitting " ," and ", " (lines 3 & 4 above) seems un-intuitive to me.
Why it matters: writing code to parse and handle a delimited string where some substrings might be empty, I'd expect split(",") to let me know how many substrings there are, but if splitting "," and ",,,,,,," return the same thing (an empty array) that just seems odd.
trailing empty substrings are not returned
https://rubyapi.org/3.4/o/string#method-i-split
>> ",,,a,,,".split(",")
=> ["", "", "", "a"]
You can set limit
as -1
to return trailing empty strings:
>> ",,,a,,,".split(",", -1)
=> ["", "", "", "a", "", "", ""]
>> ",".split(",", -1)
=> ["", ""]