lua笔记


Table of Contents

lua分割字符串

local ngx_find    = ngx.re.find

local function split(str, sep , count)
    local t = {}
    count = count or -1

    local nfield, nstart = 1, 1
    local nfirst,nlast = string.find(str, sep)
    -- local nfirst,nlast = ngx_find(str, sep, "jo")
    while nfirst and count ~= 0 do
        t[nfield] = string.sub(str, nstart, nfirst - 1)
        nfield = nfield+1
        nstart = nlast+1
        nfirst,nlast = string.find(str, sep, nstart)
        -- nfirst,nlast = ngx_find(str, sep, "jo", nil, nstart)
        count = count-1
    end
    t[nfield] = string.sub(str, nstart)
    return t
end
作者: honmaple
链接: https://honmaple.me/articles/2018/01/lua笔记.html
版权: 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat