lua 分割字符串

来源:互联网 发布:网络保险中介会倒闭吗 编辑:程序博客网 时间:2024/06/05 04:36

转自 :http://zhaiku.blog.51cto.com/2489043/1163077

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

使用例子一

local str = 'abc,123,hello,ok'local list = string.split(str, ',')for _, s in ipairs(list) do    print(s)end

结果:

abc123hellook

使用例子二

local str = 'abc \n123 \t hello ok'local list = string.split(str, '%s')for _, s in ipairs(list) do    print(s)end

结果:

abc123hellook