首页 > 其他 > 详细

lua 实现在字符之间插入指定字符

时间:2014-07-23 17:37:21      阅读:577      评论:0      收藏:0      [点我收藏+]
-- 下面的代码可以实现在字符之间插入指定字符(lua),给大家分享下,可能笨拙一些,不过解决了问题 
function ui.string_insert(str,insertStr) 
    local len = #str; 
    local left = len; 
    local cnt = 0; 
    local arr={0,0xc0,0xe0,0xf0,0xf8,0xfc}; 
    local indx = -left; 
    local newstr = ""; 
    while left ~= 0 do 
        local tmp=string.byte(str,-left); 
        local i=#arr; 
        while arr do 
            if tmp>=arr then  
                left=left-i; 
                break; 
            end 
            i=i-1;             
        end 
        local substr = string.sub(str,indx,-left - 1); 
        if left ~= 0 then 
            newstr = newstr .. substr .. insertStr; 
        else 
            newstr = newstr .. substr; 
        end 
         
         
        indx = -left; 
        cnt=cnt+1; 
    end 
    return newstr; 
end


lua 实现在字符之间插入指定字符,布布扣,bubuko.com

lua 实现在字符之间插入指定字符

原文:http://my.oschina.net/u/140714/blog/294174

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