How to get array from string contained identical symbols in Ruby?

来源:互联网 发布:黄金价格数据下载 编辑:程序博客网 时间:2024/05/05 09:13

I have string like this: "1112222355". How can I get array like that ["111","2222","3","55"] using Ruby?

Assuming you want to group only consecutive elements, use Enumerable#chunk:

> "1112222355".chars.chunk { |x| x }.map { |c, cs| cs.join }=> ["111", "2222", "3", "55"]
"1112222355".scan(/((.)\2*)/).map(&:first)# => ["111", "2222", "3", "55"] 

0 0
原创粉丝点击