p '2003,11,21,字符串,ACB'.split(/,/) #=> ["2003", "11", "21", "字符串", "ACB"] |
设置 Sting#split 的第二参数, 可以设定最大分割的个数
p '2003,11,21,字符串,ACB'.split(/,/, 3) #=> ["2003", "11", "21,字符串,ACB"] |
如果分割符也想原样保留的情况, 可以使用正则表达式的 ()
p '2003,11,21,字符串,ACB'.split(/(,)/) #=> ["2003", ",", "11", ",", "21", ",", "字符串", ",", "ACB"] |
没有评论:
发表评论