在Ruby中,我希望显示此字符串中以“a”开头的单词数:an ant hello hint how are you
答案 0 :(得分:7)
您可以将正则表达式与扫描结合使用
string.scan(/\ba/).size
答案 1 :(得分:0)
"a asa a ss sa a a a ass a ".split.select {|w| w[0] == "a"}.size
答案 2 :(得分:0)
p "an ant hello hint how are you".split.count{|word| word.start_with?('a')}
#=> 3