Count occurrences of a substring: Difference between revisions

Content added Content deleted
Line 402: Line 402:
>>> "ababababab".count("abab")
>>> "ababababab".count("abab")
2</lang>
2</lang>

=={{header|Ruby}}==
<lang ruby>irb(main):001:0> "the three truths".scan("th").count
=> 3
irb(main):002:0> "ababababab".scan("abab").count
=> 2</lang>

String#scan returns an array of substrings, and Array#count (or Array#length or Array#size) counts them.


=={{header|SNOBOL4}}==
=={{header|SNOBOL4}}==