如何将字符串转换为数组?

时间:2014-03-18 11:04:48

标签: ruby string

我有一个字符串:

countries = "India,Bangladesh,Srilanka,South Africa,Australia,Canada"

我需要从中获取一个数组:

countries = ["India","Bangladesh","Srilanka","South Africa","Australia","Canada"]

我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

使用split方法:

countries = countries.split(",")

答案 1 :(得分:2)

试试这个:

countries = "India,Bangladesh,Srilanka,South Africa,Australia,Canada".split(',')