From 83f146eef02495b83b2fad8819a55d3495dc7b03 Mon Sep 17 00:00:00 2001 From: Data-Meister Date: Thu, 21 Jan 2016 18:01:17 +0000 Subject: [PATCH 1/3] Add a recipe to count occurrences of a given string within a message --- chapters/strings/finding-substrings.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/chapters/strings/finding-substrings.md b/chapters/strings/finding-substrings.md index a33af8c..016021a 100644 --- a/chapters/strings/finding-substrings.md +++ b/chapters/strings/finding-substrings.md @@ -24,6 +24,18 @@ message.indexOf "This", 5 message.lastIndexOf "This" # => 49 + +# Count occurrences of a given string + +substrCount = (str, subStr) -> + + re = new RegExp( subStr ,"g" ) + + ( str.match(re) or [] ).length + +substrCount message, " a " +# => 3 + {% endhighlight %} ## Discussion From f56cd11a43b1e60eeaaf6a89730f3074454601fd Mon Sep 17 00:00:00 2001 From: Data-Meister Date: Thu, 21 Jan 2016 18:02:13 +0000 Subject: [PATCH 2/3] Fix indentation --- chapters/strings/finding-substrings.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/chapters/strings/finding-substrings.md b/chapters/strings/finding-substrings.md index 016021a..cc351f4 100644 --- a/chapters/strings/finding-substrings.md +++ b/chapters/strings/finding-substrings.md @@ -27,11 +27,9 @@ message.lastIndexOf "This" # Count occurrences of a given string -substrCount = (str, subStr) -> - - re = new RegExp( subStr ,"g" ) - - ( str.match(re) or [] ).length + substrCount = (str, subStr) -> + re = new RegExp( subStr ,"g" ) + ( str.match(re) or [] ).length substrCount message, " a " # => 3 From 721deae7527a006eb586996b7e5b7dbea5354806 Mon Sep 17 00:00:00 2001 From: Data-Meister Date: Thu, 21 Jan 2016 18:02:57 +0000 Subject: [PATCH 3/3] remove todo --- chapters/strings/finding-substrings.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/chapters/strings/finding-substrings.md b/chapters/strings/finding-substrings.md index cc351f4..5a070d2 100644 --- a/chapters/strings/finding-substrings.md +++ b/chapters/strings/finding-substrings.md @@ -35,7 +35,3 @@ substrCount message, " a " # => 3 {% endhighlight %} - -## Discussion - -Still need recipe to count occurrences of a given string within a message.