Count occurrences of a substring: Difference between revisions

Content added Content deleted
(Added Haskell entry.)
(added php)
Line 203: Line 203:
}
}
print countSubstring("the three truths","th"), "\n";
print countSubstring("the three truths","th"), "\n"; # prints "3"
print countSubstring("ababababab","abab"), "\n";</lang>
print countSubstring("ababababab","abab"), "\n"; # prints "2"</lang>
=={{header|Perl 6}}==
=={{header|Perl 6}}==
<lang perl6>sub count-substring($big,$little) { +$big.comb: /$little/ }
<lang perl6>sub count-substring($big,$little) { +$big.comb: /$little/ }
Line 211: Line 211:
say count-substring("ababababab","abab");</lang>
say count-substring("ababababab","abab");</lang>
Note that in Perl 6 the <tt>/$little/</tt> matches the variable literally, so there's no need to quote regex metacharacters. Also, prefix <tt>+</tt> forces numeric context in Perl&nbsp;6 (it's a no-op in Perl&nbsp;5). One other style point: we now tend to prefer hyphenated names over camelCase.
Note that in Perl 6 the <tt>/$little/</tt> matches the variable literally, so there's no need to quote regex metacharacters. Also, prefix <tt>+</tt> forces numeric context in Perl&nbsp;6 (it's a no-op in Perl&nbsp;5). One other style point: we now tend to prefer hyphenated names over camelCase.

=={{header|PHP}}==
<lang php><?php
echo substr_count("the three truths", "th"), "\n"; // prints "3"
echo substr_count("ababababab", "abab"), "\n"; // prints "2"
?></lang>


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==