最近项目里出现了大量的 scan 方法。查看了下资料,明白了它的意思。 scan 是 将满足条件(如正则表达式过滤后)的字符串返回到数组,split 是将满足条件的内容为分割点,将分割点左右两边的字符串返回到数组中。不知道这样理解是否有偏差 The output of String#split doesn't include the delimiter. Rather, everything except the delimiter would be returned in the output array, while the output of String#scan would only include what is matched by the delimiter.
"a|delimited|string".split("|")
"a|delimited|string".scan("|")
Both of the above would also accept a regular expression in place of the simple string "|".
"a|delimited|string".split(/t.+t/)
"a|delimited|string".scan(/t.+t/)