I get this result (notice that the first ""
is for the preceding empty match):
"babab".split("b")
# => ["", "a", "a"]
By replacing "a"
with an empty string in the input above as follows,
"bbb".split("b")
I expected to get the following result:
["", "", ""]
But in reality, I get:
[]
What is the logic behind this?
Logic is described in the documentation:
If the
limit
parameter is omitted, trailing null fields are suppressed.
Trailing empty fields are removed, but not leading ones.
If, by any chance, what you were asking is "yeah, but where's the logic in that?", then imagine we're parsing some CSV.
fname,sname,id,email,status
,,1,[email protected],
We want the first two position to remain empty (rather than be removed and have fname become 1 and sname - [email protected]).
We care less about trailing empty fields. Removed or kept, they don't shift data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With