Ruby字符串到字符串数组

时间:2012-08-23 11:00:37

标签: ruby arrays string

我有一个这样的字符串:

"John Godson Michael Meyer Ludovicio Sforza Cezar Borgia Alessandro Batista"

如何将此字符串转换为字符串数组,如

# ["John", "Godson", "Michael Meyer" (...)]

2 个答案:

答案 0 :(得分:6)

只需使用String#split

"John Godson Michael Meyer Ludovicio Sforza Cezar Borgia Alessandro Batista".split

答案 1 :(得分:1)

"John Godson Michael Meyer Ludovicio Sforza Cezar Borgia Alessandro Batista".split(' ')
#=> ["John", "Godson", "Michael", "Meyer", "Ludovicio", "Sforza", "Cezar", "Borgia", "Alessandro", "Batista"]