lua 分割字符串

-- 参数:待分割的字符串,分割字符  

-- 返回:子串表.(含有空串)  

function split(str, split_char)      

    local sub_str_tab = {}

    while true do          

        local pos = string.find(str, split_char) 

        if not pos then              

            table.insert(sub_str_tab,str)

            break

        end  

        local sub_str = string.sub(str, 1, pos - 1)              

        table.insert(sub_str_tab,sub_str)

        str = string.sub(str, pos + 1, string.len(str))

    end      

    return sub_str_tab

end

使用事例:

split(“hem,john”, ",")    --将逗号做为分割字符,分割后返回{[1] = "hem",[2] = "john"}

原文地址:https://www.cnblogs.com/HemJohn/p/4818032.html