How to check if a String includes another String in Elixir? This is different from How to find index of a substring?.
Something like:
String.includes("hello", "lo")
#=> true
String.contains?(string, contents)
Checks if string contains any of the given contents.
Example:
iex> String.contains? "elixir of life", "of"
true
iex> String.contains? "elixir of life", ["life", "death"]
true
iex> String.contains? "elixir of life", ["venus", "mercury"]
false
You can also use =~
:
"elixir of life" =~ "of"
# true
or in tests with assertions:
assert "elixir of life" =~ "of"
or with a regular expression:
"elixir of life" =~ ~r/of/
# true
Excerpt from the documentation: Matches the term on the left against the regular expression or string on the right.
Returns true if left matches right (if it's a regular expression) or contains right (if it's a string).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With