Count occurrences of a substring: Difference between revisions

Added Elixir
(→‎{{header|UNIX Shell}}: removed spaces)
(Added Elixir)
Line 483:
end
</lang>
 
=={{header|Elixir}}==
<lang elixir>countSubstring = fn(_, "") -> 0
(str, sub) -> length(String.split(str, sub)) - 1 end
 
data = [ {"the three truths", "th"},
{"ababababab", "abab"},
{"abaabba*bbaba*bbab", "a*b"},
{"abaabba*bbaba*bbab", "a"},
{"abaabba*bbaba*bbab", " "},
{"abaabba*bbaba*bbab", ""},
{"", "a"},
{"", ""} ]
 
Enum.each(data, fn{str, sub} ->
IO.puts countSubstring.(str, sub)
end)</lang>
 
{{out}}
<pre>
3
2
2
7
0
0
0
0
</pre>
 
=={{header|Erlang}}==
Anonymous user