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 in a video how I achieved it.
Kesimpulan. Menurut Kamus Bahasa Inggris Terjemahan Indonesia, arti kata finding adalah keputusan. Arti lainnya dari finding adalah penemuan.
Arti kata pin dalam Kamus Bahasa Inggris – Indonesia adalah kb. 1 peniti. 2 lencana. -kkt. (pinned) 1 menyematkan (a dress).
If you just want the number of a's:
puts "Melanie is a noob".count('a') #=> 2
Docs for more details.
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"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With