Lua string.sub截取UTF8 中英混合字符

在使用Lua的过程中,经常会遇到需要截取字符串或者获得字符串真实长度的情况,而Lua自带的string.sub()对于中文字符会当作3个字符来处理,截取时会造成乱码。所以需要自己改造下,下面的SubStringUTF8()方法是我改编的SubString方法,经测试可以识别中英混合的字符串,不管是英文字符还是中文字符都当作一个字符来计算index,并且可以像系统的string.sub()一样使用负数来从末尾截取字符。其他的几个方法是SubStringUTF8()的依赖方法,也可以单独拿来使用。

 1 --截取中英混合的UTF8字符串,endIndex可缺省
 2 function SubStringUTF8(str, startIndex, endIndex)
 3     if startIndex < 0 then
 4         startIndex = SubStringGetTotalIndex(str) + startIndex + 1;
 5     end
 6 
 7     if endIndex ~= nil and endIndex < 0 then
 8         endIndex = SubStringGetTotalIndex(str) + endIndex + 1;
 9     end
10 
11     if endIndex == nil then 
12         return string.sub(str, SubStringGetTrueIndex(str, startIndex));
13     else
14         return string.sub(str, SubStringGetTrueIndex(str, startIndex), SubStringGetTrueIndex(str, endIndex + 1) - 1);
15     end
16 end
17 
18 --获取中英混合UTF8字符串的真实字符数量
19 function SubStringGetTotalIndex(str)
20     local curIndex = 0;
21     local i = 1;
22     local lastCount = 1;
23     repeat 
24         lastCount = SubStringGetByteCount(str, i)
25         i = i + lastCount;
26         curIndex = curIndex + 1;
27     until(lastCount == 0);
28     return curIndex - 1;
29 end
30 
31 function SubStringGetTrueIndex(str, index)
32     local curIndex = 0;
33     local i = 1;
34     local lastCount = 1;
35     repeat 
36         lastCount = SubStringGetByteCount(str, i)
37         i = i + lastCount;
38         curIndex = curIndex + 1;
39     until(curIndex >= index);
40     return i - lastCount;
41 end
42 
43 --返回当前字符实际占用的字符数
44 function SubStringGetByteCount(str, index)
45     local curByte = string.byte(str, index)
46     local byteCount = 1;
47     if curByte == nil then
48         byteCount = 0
49     elseif curByte > 0 and curByte <= 127 then
50         byteCount = 1
51     elseif curByte>=192 and curByte<=223 then
52         byteCount = 2
53     elseif curByte>=224 and curByte<=239 then
54         byteCount = 3
55     elseif curByte>=240 and curByte<=247 then
56         byteCount = 4
57     end
58     return byteCount;
59 end
原文地址:https://www.cnblogs.com/atong/p/6298078.html