Skip to content

Commit

Permalink
Merge pull request #139 from Data-Meister/master
Browse files Browse the repository at this point in the history
Faster, simpler way to count occurrences of a given string
  • Loading branch information
lolmaus committed Jan 28, 2016
2 parents 9d67a39 + d6da838 commit 23eb90c
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions chapters/strings/finding-substrings.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,8 @@ 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 "
message.split(" a ").length - 1
# => 3

{% endhighlight %}

0 comments on commit 23eb90c

Please sign in to comment.