From f1a6aecb2bf4fca28568060ef2c21ba7349db9cd Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 6 Sep 2016 14:31:19 -0500 Subject: [PATCH] Add Check For A Substring Match as an elixir til --- README.md | 3 ++- elixir/check-for-a-substring-match.md | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 elixir/check-for-a-substring-match.md diff --git a/README.md b/README.md index a033129..862ebb5 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really warrant a full blog post. These are mostly things I learn by pairing with smart people at [Hashrocket](http://hashrocket.com/). -_464 TILs and counting..._ +_465 TILs and counting..._ --- @@ -84,6 +84,7 @@ _464 TILs and counting..._ - [Append To A Keyword List](elixir/append-to-a-keyword-list.md) - [Assert An Exception Is Raised](elixir/assert-an-exception-is-raised.md) - [Binary Representation Of A String](elixir/binary-representation-of-a-string.md) +- [Check For A Substring Match](elixir/check-for-a-substring-match.md) - [Create A Date With The Date Sigil](elixir/create-a-date-with-the-date-sigil.md) - [Do You Have The Time?](elixir/do-you-have-the-time.md) - [Documentation Lookup With Vim And Alchemist](elixir/documentation-lookup-with-vim-and-alchemist.md) diff --git a/elixir/check-for-a-substring-match.md b/elixir/check-for-a-substring-match.md new file mode 100644 index 0000000..bcfb628 --- /dev/null +++ b/elixir/check-for-a-substring-match.md @@ -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)