I\'m trying to count the number of times a string appears in another string.
I know you can count the number of times a letter appears in a string:
strin
It's because the count counts characters, not instances of strings. In this case 'aa'
means the same thing as 'a'
, it's considered a set of characters to count.
To count the number of times aa
appears in the string:
string = "aabbccddbb"
string.scan(/aa/).length
# => 1
string.scan(/bb/).length
# => 2
string.scan(/ff/).length
# => 0