diff --git a/lib/elixir/lib/string.ex b/lib/elixir/lib/string.ex index 79178427ec..2acbf6408d 100644 --- a/lib/elixir/lib/string.ex +++ b/lib/elixir/lib/string.ex @@ -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) @@ -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