Count occurrences of a substring: Difference between revisions

→‎{{header|Perl}}: don't apologize for not handling metachars when you can just handle 'em :)
(→‎{{header|Perl}}: Added PureBasic)
(→‎{{header|Perl}}: don't apologize for not handling metachars when you can just handle 'em :))
Line 37:
 
=={{header|Perl}}==
This solution uses regex, hence the substring cannot contain regex metacharacters
<lang perl>sub countSubstring {
my ($str, $sub) = @_shift;
my $sub = quotemeta(shift);
my $count = () = $str =~ /$sub/g;
return $count;
Line 47:
print countSubstring("the three truths","th"), "\n";
print countSubstring("ababababab","abab"), "\n";</lang>
 
=={{header|PureBasic}}==
<lang PureBasic>a = CountString("the three truths","th")
Anonymous user