加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua实现split的简易方法

发布时间:2020-12-14 22:21:26 所属栏目:大数据 来源:网络整理
导读:刚想用lua的split函数,网上查了下,实现都比较复杂,自己写了个:) string.split = function(s,p) ?? ?local rt= {} ?? ?string.gsub(s,'[^'..p..']+',function(w) table.insert(rtlist,w) end ) ?? ?return rt end 使用例子一 local str = 'abc,123,hello

刚想用lua的split函数,网上查了下,实现都比较复杂,自己写了个:)

string.split = function(s,p)

?? ?local rt= {}
?? ?string.gsub(s,'[^'..p..']+',function(w) table.insert(rtlist,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 n123 t hello ok'

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

for _,s in ipairs(list) do

??? print(s)

end


结果:

abc

123

hello

ok

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读