1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Check For A Substring Match as an elixir til

This commit is contained in:
jbranchaud
2016-09-06 14:31:19 -05:00
parent 1ef2367f65
commit f1a6aecb2b
2 changed files with 22 additions and 1 deletions

View File

@@ -0,0 +1,20 @@
# Check For A Substring Match
Using Erlang's `:binary.match` function, you can easily check if a string
has a matching substring.
```elixir
> :binary.match("all food is good", "foo")
{4, 3}
> :binary.match("all food is good", "bar")
:nomatch
```
As you can see, the return value on a successful match is a tuple with the
index of where the match starts and the length of the match. If there is no
match, the `:nomatch` atom is returned.
See the [`match/2` and `match/3`
docs](http://erlang.org/doc/man/binary.html#match-2) for more details.
[source](http://stackoverflow.com/questions/35551072/how-to-find-index-of-a-substring)