How to split a delimited string in Ruby and convert it to an array?

前端 未结 5 1462
眼角桃花
眼角桃花 2020-12-12 10:18

I have a string

\"1,2,3,4\"

and I\'d like to convert it into an array:

[1,2,3,4]

How?

5条回答
  •  余生分开走
    2020-12-12 10:49

    "12345".each_char.map(&:to_i)
    

    each_char does basically the same as split(''): It splits a string into an array of its characters.

    hmmm, I just realize now that in the original question the string contains commas, so my answer is not really helpful ;-(..

提交回复
热议问题