Good question! The implementation of String.split/3 is such that matches of
the splitting pattern are discarded. For instance:
iex(1)> String.split("hello world", ~r/\s/)
["hello", "world"]
iex(2)> String.split("hello\tworld", ~r/\s/)
["hello", "world"]
iex(3)> String.split("hello\nworld", ~r/\s/)
["hello", "world"]
The output doesn't include the whitespace the string was split on, and thus
a problem such as "truncate string to x number of words/characters *while
preserving whitespace*" cannot be easily solved this way.
On Wednesday, December 2, 2020 at 1:52:12 PM UTC-5 [email protected]
wrote:
> Are there use-cases that you see for this feature that don't fall under
> String.split/3 with a regex argument?
>
> https://hexdocs.pm/elixir/String.html#split/3
> On Tuesday, December 1, 2020 at 11:37:20 AM UTC-6 [email protected]
> wrote:
>
>>
>> This is a generalization of the existing (and oddly specific)
>> String.chunk/2 function that takes a string and a single-argument predicate
>> function, returning a list of strings.
>>
>> e.g.
>> String.chunk_by(" foo bar ", &(&1 =~ ~r/\w/))
>> # => [" ", "foo", " ", "bar", " "]
>>
>> The above example makes problems such as "wrap text at <line limit>
>> preserving whitespace" or "truncate string to x number of words while
>> preserving whitespace" simpler.
>>
>> The actual change would be pretty small and consist of renaming `chunk`
>> to `chunk_by`, removing a line, and re-defining `chunk` as a slightly
>> specialized call to `chunk_by`
>>
>
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/706bd17d-7a4f-41c6-bcbf-07584badfb8en%40googlegroups.com.