lua 计算utf8字符串的长度

写到前面的话:
作者从毕业开始一直从事游戏开发,提供大量游戏实战模块代码及案例供大家学习与交流,希望以下知识可以带来一些帮助,如有任何疑问,请加群641792143交流与学习

 function utf8len(input)
        local len  = string.len(input)
        local left = len
        local cnt  = 0
        local arr  = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}
        while left ~= 0 do
            local tmp = string.byte(input, -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
    
    local title = "wo是男的12"
    
    print(utf8len(title))

转载自 https://blog.csdn.net/heyuchang666/article/details/51832304

发布了43 篇原创文章 · 获赞 1 · 访问量 2331

猜你喜欢

转载自blog.csdn.net/lpl312905509/article/details/94125229