Skip to content

String.split / Regex.split trim behavior with empty patterns #1688

@bruce

Description

@bruce

Both String.split and Regex.split (which the former relies on) have the following behavior when provided an empty pattern:

iex> String.split("abc", %r//)
["a", "b", "c", ""]
iex> Regex.split(%r//, "abc")
["a", "b", "c", ""]

While I understand we can provide the :trim option and that this behavior matches Erlang's re module, namely:

[This] leaves an empty rest which is also returned. This behaviour differs from the default behaviour of the split function in Perl, where empty strings at the end are by default removed.

I thought it might make sense to discuss whether Elixir's more friendly (dare I say "modern") approach might provide an opening to change this to the more conventional behavior that I think most would expect. (I still don't understand why Erlang doesn't also return an empty value at the beginning of the result list.)

If the decision is made not to change this behavior, perhaps this behavior should be clearly stated in the Elixir docs, with doctest-able examples included.

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions