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

来源:互联网 发布:大数据视频教程 编辑:程序博客网 时间:2024/05/17 18:16

声明:本文从别处拷贝而来,感谢原文作者的分享。

function 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[i] 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
str = 'abcdefg'insertStr = ','newstr:a,b,c,d,e,f,g
0 0
原创粉丝点击