Lua分割字符串

-- 用指定字符串切割另一个字符串
local function splitStr(str, delimeter)
    local find, sub, insert = string.find, string.sub, table.insert
    local res = {}
    local start, start_pos, end_pos = 1, 1, 1
    while true do
        start_pos, end_pos = find(str, delimeter, start, true)
        if not start_pos then
            break
        end
        insert(res, sub(str, start, start_pos - 1))
        start = end_pos + 1
    end
    insert(res, sub(str,start))
    return res
end

local function print_r(arr)
    for k,v in pairs(arr) do
        io.write("'" .. v .. "'" .. "\t")
    end
    io.write("\n")
end

local list = splitStr('4.12.45','.')

print("---"..list[3])

if tonumber(list[1]) > 5 then 

    print(">>>>>")
else
    print("<<<<<")
end

你可能感兴趣的:(Lua分割字符串)