Count occurrences of a substring: Difference between revisions

Content added Content deleted
(→‎{{header|Groovy}}: new solution)
Line 280: Line 280:
2
2
2</pre>
2</pre>
=={{header|Groovy}}==
Solution, uses the Groovy "match" operator (=~), and the Groovy-extended Matcher property "count":
<lang groovy>println (('the three truths' =~ /th/).count)
println (('ababababab' =~ /abab/).count)
println (('abaabba*bbaba*bbab' =~ /a*b/).count)
println (('abaabba*bbaba*bbab' =~ /a\*b/).count)</lang>

Output:
<pre>3
2
9
2</pre>

=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>
<lang haskell>