拆分字串

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"]