Skip to content

Commit 8e61baa

Browse files
committed
Fix String.split/3 example in guide and add note in docs (#14223)
1 parent 7426acb commit 8e61baa

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

lib/elixir/lib/string.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,15 @@ defmodule String do
488488
iex> String.split(String.normalize("é", :nfc), "e")
489489
["é"]
490490
491+
When using both the `:trim` and the `:parts` option, the empty values
492+
are removed as the parts are computed (if any). No trimming happens
493+
after all parts are computed:
494+
495+
iex> String.split(" a b c ", " ", trim: true, parts: 2)
496+
["a", " b c "]
497+
iex> String.split(" a b c ", " ", trim: true, parts: 3)
498+
["a", "b", " c "]
499+
491500
"""
492501
@spec split(t, pattern | Regex.t(), keyword) :: [t]
493502
def split(string, pattern, options \\ [])

lib/elixir/pages/getting-started/keywords-and-maps.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,44 +11,48 @@ Keyword lists are a data-structure used to pass options to functions. Let's see
1111
Imagine you want to split a string of numbers. Initially, we can invoke `String.split/2` passing two strings as arguments:
1212

1313
```elixir
14-
iex> String.split("1 2 3", " ")
15-
["1", "2", "3"]
14+
iex> String.split("1 2 3 4", " ")
15+
["1", "2", "3", "4"]
1616
```
1717

18-
However, what happens if there is an additional space between the numbers:
18+
What if you only want to split at most 2 times? The `String.split/3` function allows the `parts` option to be set to the maximum number of entries in the result:
1919

2020
```elixir
21-
iex> String.split("1 2 3", " ")
22-
["1", "", "2", "", "3"]
21+
iex> String.split("1 2 3 4", " ", [parts: 3])
22+
["1", "2", "3 4"]
2323
```
2424

25-
As you can see, there are now empty strings in our results. Luckily, the `String.split/3` function allows the `trim` option to be set to true:
25+
As you can see, we got 3 parts, the last one containing the remaining of the input without splitting it.
26+
27+
Now imagine that some of the inputs you must split on contains additional spaces between the numbers:
2628

2729
```elixir
28-
iex> String.split("1 2 3", " ", [trim: true])
29-
["1", "2", "3"]
30+
iex> String.split("1 2 3 4", " ", [parts: 3])
31+
["1", "", "2 3 4"]
3032
```
3133

32-
We can also use options to limit the splitting algorithm to a maximum number of parts, as shown next:
34+
As you can see, the additional spaces lead to empty entries in the output. Luckily, we can also set the `trim` option to `true` to remove them:
3335

3436
```elixir
35-
iex> String.split("1 2 3", " ", [trim: true, parts: 2])
36-
["1", "2 3"]
37+
iex> String.split("1 2 3 4", " ", [parts: 3, trim: true])
38+
["1", "2", " 3 4"]
3739
```
3840

39-
`[trim: true]` and `[trim: true, parts: 2]` are keyword lists. When a keyword list is the last argument of a function, we can skip the brackets and write:
41+
Once again we got 3 parts, with the last one containing the leftovers.
42+
43+
`[parts: 3]` and `[parts: 3, trim: true]` are keyword lists. When a keyword list is the last argument of a function, we can skip the brackets and write:
4044

4145
```elixir
42-
iex> String.split("1 2 3", " ", trim: true, parts: 2)
43-
["1", "2 3"]
46+
iex> String.split("1 2 3 4", " ", parts: 3, trim: true)
47+
["1", "2", " 3 4"]
4448
```
4549

4650
As shown in the example above, keyword lists are mostly used as optional arguments to functions.
4751

4852
As the name implies, keyword lists are simply lists. In particular, they are lists consisting of 2-item tuples where the first element (the key) is an atom and the second element can be any value. Both representations are the same:
4953

5054
```elixir
51-
iex> [{:trim, true}, {:parts, 2}] == [trim: true, parts: 2]
55+
iex> [{:parts, 3}, {:trim, true}] == [parts: 3, trim: true]
5256
true
5357
```
5458

0 commit comments

Comments
 (0)