刚看到一段LUA 代码.简洁.优美.看明白以后真是佩服死了
lua吧
全部回复
仅看楼主
level 1
-- 分隔字符串
function string:split(sep)
local sep, fields = sep or "\t", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end

2012年07月10日 08点07分 1
level 1
很平常啊,table deep copy ,还有伪class构造函数什么的 都有这样风格
2012年07月20日 14点07分 2
level 1
话是这么说.不过请搜一下网上的LUA split实现.你就会发现这个是最简洁优美的.有些同样功能的例子.代码量长出3,4倍出去.
2012年07月24日 01点07分 3
level 1
我会到处乱说有正则表达式这种东西吗
2012年08月19日 16点08分 4
这个也用到了.
2012年08月21日 03点08分
level 8
[瞌睡]
2012年08月20日 10点08分 5
level 10
学了半天的LUA感觉还好, 不难. 不过lz发的现在还是看不懂昂?
2012年10月31日 13点10分 7
level 14
string.split = function(str, sep)
local fields = {}
str:gsub("[^"..sep.."]+", function(c) fields[#fields+1] = c end)
return fields
end
2012年11月02日 12点11分 8
level 1
function split(s, p)
local retList = {}
gsub(s,'[^'..p..']+',function(w) table.insert(retList,w) end)
return retList
end
集思广益
2012年12月04日 14点12分 9
这段代码是什么意思?求解[泪]
2016年04月19日 07点04分
level 4
楼主和楼下的方法其实都有问题
比如a,,b,c
用,分隔应该得到 "a" "" "b" "c" 四个部分才对
另外,
a+-b-+c
我要用+-分隔,应该得到 "a" "b-+c"
2014年07月31日 03点07分 13
回复 矮番薯_爱翻书 :[汗]不是我挖的
2014年08月01日 00点08分
level 2
lua split用正则用必然会出错的...还是得用find,sub做
2014年10月31日 07点10分 14
挖坟[阴险]
2014年10月31日 09点10分
level 11
等等,lua不是不支持正则表达式吗?……
2014年10月31日 08点10分 15
level 1
string.split = function(s, p)
local rt= {}
string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
return rt
end
2017年01月21日 03点01分 16
1