From d6da838694e611a73822cdaf667ec5b9530260f3 Mon Sep 17 00:00:00 2001 From: Data-Meister Date: Thu, 28 Jan 2016 17:56:24 +0000 Subject: [PATCH] Faster, simpler way to count occurrences of a given string --- chapters/strings/finding-substrings.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/chapters/strings/finding-substrings.md b/chapters/strings/finding-substrings.md index 5a070d2..ee81e89 100644 --- a/chapters/strings/finding-substrings.md +++ b/chapters/strings/finding-substrings.md @@ -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 %}