可以将字符串转换为数组?

use*_*657 23 ruby arrays string

# (this give me list eg : ,s, s, m ...
list = list + "," + name
Run Code Online (Sandbox Code Playgroud)

我想使用拆分然后加入...尝试使用下面..我不知道我在做什么

list = list.split(',') + name + collect{|c| c.name}.to_sentence 
list = list.split(',') + name + collect{|c| c.name}.Join(",")
Run Code Online (Sandbox Code Playgroud)

有任何想法吗??

Jas*_*Kim 45

我注意到这个页面出现了"string to char array ruby​​"的通用谷歌搜索.

如果你只想在Ruby中将字符串转换为char数组,

"string".chars
# ["s", "t", "r", "i", "n", "g"]
Run Code Online (Sandbox Code Playgroud)

  • 简短而甜蜜,正是我所需要的 (3认同)

kur*_*umi 19

我不明白你的问题,但如果你想将字符串转换为数组

>> "a string".split("")
=> ["a", " ", "s", "t", "r", "i", "n", "g"]
Run Code Online (Sandbox Code Playgroud)