Skip to content

Commit d6da838

Browse files
committed
Faster, simpler way to count occurrences of a given string
1 parent 9d67a39 commit d6da838

File tree

1 file changed

+1
-7
lines changed

1 file changed

+1
-7
lines changed

chapters/strings/finding-substrings.md

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

27-
2827
# Count occurrences of a given string
29-
30-
substrCount = (str, subStr) ->
31-
re = new RegExp( subStr ,"g" )
32-
( str.match(re) or [] ).length
33-
34-
substrCount message, " a "
28+
message.split(" a ").length - 1
3529
# => 3
3630

3731
{% endhighlight %}

0 commit comments

Comments
 (0)