Skip to content

Commit 9d67a39

Browse files
committed
Merge branch 'Data-Meister-master'
Closes PR coffeescript-cookbook#137 This was a squashed rebase of the following: commit 721deae Author: Data-Meister <[email protected]> Date: Thu Jan 21 18:02:57 2016 +0000 remove todo commit f56cd11 Author: Data-Meister <[email protected]> Date: Thu Jan 21 18:02:13 2016 +0000 Fix indentation commit 83f146e Author: Data-Meister <[email protected]> Date: Thu Jan 21 18:01:17 2016 +0000 Add a recipe to count occurrences of a given string within a message
2 parents 2fd9c30 + 202bdfd commit 9d67a39

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

chapters/strings/finding-substrings.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ message.indexOf "This", 5
2424
message.lastIndexOf "This"
2525
# => 49
2626

27-
{% endhighlight %}
2827

29-
## Discussion
28+
# Count occurrences of a given string
29+
30+
substrCount = (str, subStr) ->
31+
re = new RegExp( subStr ,"g" )
32+
( str.match(re) or [] ).length
3033

31-
Still need recipe to count occurrences of a given string within a message.
34+
substrCount message, " a "
35+
# => 3
36+
37+
{% endhighlight %}

0 commit comments

Comments
 (0)