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

保留小数后n位

local function round(num, n)
    local p = math.pow(10, n or 3)
    local m = num * p
    local _, d = math.modf(m)
    if d < 0.5 then return math.floor(m) / p end
    return math.ceil(m) / p
end

随机字符串

https://gist.github.com/haggen/2fd643ea9a261fea2094

作者: honmaple
链接: https://honmaple.me/articles/2018/01/lua笔记.html
版权: CC BY-NC-SA 4.0 知识共享署名-非商业性使用-相同方式共享4.0国际许可协议
wechat
alipay

加载评论