(转) lua实现split的简易方法

string.split = function(s, p)

    local rt= {}
    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
    return rt

end

使用例子一

local str = 'abc,123,hello,ok'

local list = string.split(str, ',')

for _, s in ipairs(list) do

    print(s)

end 

结果:

abc

123

hello

ok 

使用例子二

local str = 'abc  123   hello ok'

local list = string.split(str, '%s')

for _, s in ipairs(list) do

    print(s)

end 

结果:

abc

123

hello

ok

原文地址:https://www.cnblogs.com/sierllen/p/3336660.html