– 判断utf8字符byte长度
– 0xxxxxxx - 1 byte
– 110yxxxx - 192, 2 byte
– 1110yyyy - 225, 3 byte
– 11110zzz - 240, 4 byte
local function chsize(char)
if not char then
print(“not char”)
return 0
elseif char > 240 then
return 4
elseif char > 225 then
return 3
elseif char > 192 then
return 2
else
return 1
end
end

-- 计算utf8字符串字符数, 各种字符都按一个字符计算
-- 例如utf8len("1你好") => 3
function utf8len(str)
    local len = 0
    local currentIndex = 1
    while currentIndex <= #str do
	    local char = string.byte(str, currentIndex)
	    currentIndex = currentIndex + chsize(char)
	    len = len +1
    end
    return len
end

– 截取utf8 字符串
– str: 要截取的字符串
– startChar: 开始字符下标,从1开始
– numChars:要截取的字符长度
function utf8sub(str, startChar, numChars)
local startIndex = 1
while startChar > 1 do
local char = string.byte(str, startIndex)
startIndex = startIndex + chsize(char)
startChar = startChar - 1
end

local currentIndex = startIndex

while numChars &gt; 0 and currentIndex &lt;= #str do
    local char = string.byte(str, currentIndex)
    currentIndex = currentIndex + chsize(char)
    numChars = numChars -1
end
return str:sub(startIndex, currentIndex - 1)

end

– 自测
function test()
– test utf8len
assert(utf8len("你好1世界哈哈") == 7)
assert(utf8len("你好世界1哈哈 ") == 8)
assert(utf8len(" 你好世 界1哈哈") == 9)
assert(utf8len("12345678") == 8)
assert(utf8len("øpø你好pix") == 8)

-- test utf8sub
assert(utf8sub(&#34;你好1世界哈哈&#34;,2,5) == &#34;好1世界哈&#34;)
assert(utf8sub(&#34;1你好1世界哈哈&#34;,2,5) == &#34;你好1世界&#34;)
assert(utf8sub(&#34; 你好1世界 哈哈&#34;,2,6) == &#34;你好1世界 &#34;)
assert(utf8sub(&#34;你好世界1哈哈&#34;,1,5) == &#34;你好世界1&#34;)
assert(utf8sub(&#34;12345678&#34;,3,5) == &#34;34567&#34;)
assert(utf8sub(&#34;øpø你好pix&#34;,2,5) == &#34;pø你好p&#34;)

print(&#34;all test succ&#34;)

end

test()


Please Star this Project if you like it! Follow would also be appreciated!
Peace!

            <hr/>
            
            

            <br/>

            
            
            

            <br/>
            
            
                
                
                <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css"/>
                <script src="https://cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>
            
            
            <br/>