当前位置: 移动技术网 > IT编程>脚本编程>Lua > Lua获取utf8字符串长度和字符串截取并用...代替

Lua获取utf8字符串长度和字符串截取并用...代替

2017年12月08日  | 移动技术网IT编程  | 我要评论

一、lua获取utf8字符串长度

复制代码 代码如下:

--- 获取utf8编码字符串正确长度的方法
-- @param str
-- @return number
function utfstrlen(str)
local len = #str;
local left = len;
local cnt = 0;
local arr={0,0xc0,0xe0,0xf0,0xf8,0xfc};
while left ~= 0 do
local tmp=string.byte(str,-left);
local i=#arr;
while arr[i] do
if tmp>=arr[i] then left=left-i;break;end
i=i-1;
end
cnt=cnt+1;
end
return cnt;
end

二、截取给定字符串的部分长度,超出部分用省略号“.”替换

复制代码 代码如下:

--@brief 切割字符串,并用“...”替换尾部
--@param sname:要切割的字符串
--@return nmaxcount,字符串上限,中文字为2的倍数
--@param nshowcount:显示英文字个数,中文字为2的倍数,可为空
--@note         函数实现:截取字符串一部分,剩余用“...”替换

function getshortname(sname,nmaxcount,nshowcount)
    if sname == nil or nmaxcount == nil then
        return
    end
    local sstr = sname
    local tcode = {}
    local tname = {}
    local nleninbyte = #sstr
    local nwidth = 0
    if nshowcount == nil then
       nshowcount = nmaxcount - 3
    end
    for i=1,nleninbyte do
        local curbyte = string.byte(sstr, i)
        local bytecount = 0;
        if curbyte>0 and curbyte<=127 then
            bytecount = 1
        elseif curbyte>=192 and curbyte<223 then
            bytecount = 2
        elseif curbyte>=224 and curbyte<239 then
            bytecount = 3
        elseif curbyte>=240 and curbyte<=247 then
            bytecount = 4
        end
        local char = nil
        if bytecount > 0 then
            char = string.sub(sstr, i, i+bytecount-1)
            i = i + bytecount -1
        end
        if bytecount == 1 then
            nwidth = nwidth + 1
            table.insert(tname,char)
            table.insert(tcode,1)
           
        elseif bytecount > 1 then
            nwidth = nwidth + 2
            table.insert(tname,char)
            table.insert(tcode,2)
        end
    end
   
    if nwidth > nmaxcount then
        local _sn = ""
        local _len = 0
        for i=1,#tname do
            _sn = _sn .. tname[i]
            _len = _len + tcode[i]
            if _len >= nshowcount then
                break
            end
        end
        sname = _sn .. "..."
    end
    return sname
end

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网