Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/elixir/lib/string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,15 @@ defmodule String do
iex> String.count("hello world", "")
12

The `pattern` can also be a compiled pattern:
The `pattern` can also be a list:

iex> String.count("hello world", ["e", "o"])
3

iex> String.count("hello world", [])
0

Or a compiled pattern:

iex> pattern = :binary.compile_pattern([" ", "!"])
iex> String.count("foo bar baz!!", pattern)
Expand All @@ -756,6 +764,8 @@ defmodule String do
@doc since: "1.19.0"
def count(string, <<>>), do: length(string) + 1

def count(_string, []), do: 0

def count(string, pattern) when is_struct(pattern, Regex) do
Kernel.length(Regex.scan(pattern, string, return: :index))
end
Expand Down
Loading