Lua String 的扩展函数

字符串分割函数返回 Table

string.split = function(line, sep)

    sep = sep or ' '
    local retval = {}
    local pos = 1
    while true do
        local from, to = string.find(line, sep, pos)
        if from then
            local item = string.sub(line, pos, from-1)
            table.insert( retval, item )
            pos = to + 1
        else
            local item = string.sub(line, pos)
            table.insert( retval, item )
            break
        end
    end
    return retval
end

猜你喜欢

转载自blog.csdn.net/ooomyself/article/details/50725223