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