Finding # occurrences of a character in a string in Ruby

前端 未结 3 1036
情书的邮戳
情书的邮戳 2021-01-30 15:28

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.

3条回答
  •  心在旅途
    2021-01-30 15:57

    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"]
    

提交回复
热议问题