110

I'm looking for the Ruby method (1.9...) that can help me find the number of occurrences of a character in a string. I'm looking for all occurrences, not just the first one.

For example: "Melanie is a noob" There are two occurrences of the letter 'a'. What would be the Ruby method I could use in order to find this?

I've been using Ruby-doc.org as a reference and the scan method in the String: class caught my eye. The wording is a bit difficult for me to understand, so I don't really grasp the concept of scan.

Edit: I was able to solve this using scan. I shared a few solutions in a video on how I achieved it.

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50
  • 2
    Do you want the number of occurrences, or the index values of the occurrences in the string? Knowing how many is often useful, but usually the next question is "where are they?" – the Tin Man Mar 23 '11 at 21:30

3 Answers3

153

If you just want the number of a's:

puts "Melanie is a noob".count('a')  #=> 2

Docs for more details.

Jan Klimo
  • 4,643
  • 2
  • 36
  • 42
steenslag
  • 79,051
  • 16
  • 138
  • 171
  • why it doesn't work with dots? Example "voyage.localhost.com".count('www.') => 2. How this can be? – Gediminas Šukys Feb 12 '14 at 05:04
  • 13
    @Gediminas `count` counts chars, not strings. "voyage.localhost.com".count('www.') is the same as "voyage.localhost.com".count('w.') and since there are no w's and two dots the result is 2. – steenslag Feb 12 '14 at 11:17
  • 3
    Any chance you could add [a link to the docs](http://ruby-doc.org/core-2.3.0/String.html#method-i-count)? – Nic Mar 14 '16 at 16:31
  • 3
    I was initially quite shocked by this answer, then I saw that Melanie used the example first. Good answer! – AJFaraday Jan 24 '17 at 11:41
  • @Hubro and AJFaraday: Ha! Yeah, I'd been _really_ impressed if, when the OP said "occurrences of a character", she meant "character" as a pun _and_ she used a sample string like, oh let's say "Trump" - you know, such that the "occurrences of _a_ character" in "Trump" would be 0. ;) – Tom Apr 07 '17 at 00:00
  • I love Ruby for things like this method! Simple and useful :) – Michał Zalewski Feb 25 '18 at 17:30
55

This link from a question asked previously should help scanning a string in Ruby

scan returns all the occurrences of a string in a string as an array, so

"Melanie is a noob".scan(/a/)

will return

["a","a"]
Community
  • 1
  • 1
Shiv
  • 8,362
  • 5
  • 29
  • 32
1

I was able to solve this by passing a string through scan as shown in another answer.

For example:

string = 'This is an example'
puts string.count('e')

Outputs:

2

I was also able to pull the occurrences by using scan and passing a sting through instead of regex which varies slightly from another answer but was helpful in order to avoid regex.

string = 'This is an example'
puts string.scan('e')

Outputs:

['e','e']

I explored these methods further in a video I created after I figured it out.

Melanie Palen
  • 2,645
  • 6
  • 31
  • 50