拆分字符串

String#split 根据分隔符将 String 分成 Array

"alpha,beta".split(",")
# => ["alpha", "beta"]

一个空的 String 导致空的 Array

"".split(",")
# => []

不匹配的分隔符会导致 Array 包含单个项目:

"alpha,beta".split(".")
# => ["alpha,beta"]

你还可以使用正则表达式拆分字符串:

"alpha, beta,gamma".split(/, ?/)
# => ["alpha", "beta", "gamma"]

分隔符是可选的,默认情况下,字符串在空格上分割:

"alpha beta".split
# => ["alpha", "beta"]