|
发表于 2017-4-30 19:39:23
|
显示全部楼层
function split(str, delim, maxNb)
if str~=nil and str~="" then
if string.find(str, delim) == nil then
return { str }
end
if maxNb == nil or maxNb < 1 then
maxNb = 0 -- No limit
end
local result = {}
local pat = "(.-)" .. delim .. "()"
local nb = 0
local lastPos
for part, pos in string.gmatch(str, pat) do
nb = nb + 1
result[nb] = part
lastPos = pos
if nb == maxNb then break end
end
if nb ~= maxNb then
result[nb + 1] = string.sub(str, lastPos)
end
return result
else
return {}
end
end
需要split函数 |
|