提交 819396c5 authored 作者: Andrew Thompson's avatar Andrew Thompson

trim() and split() functions I use in other lua scripts

上级 75472c58
-- the python-like example from http://lua-users.org/wiki/SplitJoin
function string:split(sSeparator, nMax, bRegexp)
assert(sSeparator ~= '')
assert(nMax == nil or nMax >= 1)
local aRecord = {}
if self:len() > 0 then
local bPlain = not bRegexp
nMax = nMax or -1
local nField=1 nStart=1
local nFirst,nLast = self:find(sSeparator, nStart, bPlain)
while nFirst and nMax ~= 0 do
aRecord[nField] = self:sub(nStart, nFirst-1)
nField = nField+1
nStart = nLast+1
nFirst,nLast = self:find(sSeparator, nStart, bPlain)
nMax = nMax-1
end
aRecord[nField] = self:sub(nStart)
end
return aRecord
end
-- trim9 from http://lua-users.org/wiki/StringTrim
local _util_lib_find = string.find
local _util_lib_sub = string.sub
function trim(s)
local _,i1 = _util_lib_find(s,'^%s*')
local i2 = _util_lib_find(s,'%s*$')
return _util_lib_sub(s,i1+1,i2-1)
end
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论