首页 > 其他 > 详细

lua 分割字符串

时间:2015-09-18 00:35:57      阅读:479      评论:0      收藏:0      [点我收藏+]

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

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

function cc.exports.lua_string_split(str, split_char)      

    local sub_str_tab = {};  

 

    while (true) do          

        local pos = string.find(str, split_char);    

        if (not pos) then              

            local size_t = table.getn(sub_str_tab)  

            table.insert(sub_str_tab,size_t+1,str);  

            break;    

        end  

 

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

        local size_t = table.getn(sub_str_tab)  

        table.insert(sub_str_tab,size_t+1,sub_str);  

        local t = string.len(str);  

        str = string.sub(str, pos + 1, t);     

    end      

    return sub_str_tab;  

end

 

使用事例:

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

lua 分割字符串

原文:http://www.cnblogs.com/HemJohn/p/4818032.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!