Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在 Ruby 中,我想从这个字符串中显示以“a”开头的单词数:an ant hello hint how are you
an ant hello hint how are you
您可以将正则表达式与扫描一起使用
string.scan(/\ba/).size
p "an ant hello hint how are you".split.count{|word| word.start_with?('a')} #=> 3
"a asa a ss sa a a a ass a ".split.select {|w| w[0] == "a"}.size