在使用Lua的过程中,经常会遇到需要截取字符串或者获得字符串真实长度的情况,而Lua自带的string.sub()对于中文字符会当作3个字符来处理,截取时会造成乱码。所以需要自己改造下,下面的SubStringUTF8()方法是我改编的SubString方法,经测试可以识别中英混合的字符串,不管是英文字符还是中文字符都当作一个字符来计算index,并且可以像系统的string.sub()一样使用负数来从末尾截取字符。其他的几个方法是SubStringUTF8()的依赖方法,也可以单独拿来使用。
function SubStringUTF8(str, startIndex, endIndex)
if startIndex < 0 then
startIndex = SubStringGetTotalIndex(str) + startIndex + 1;
end
if endIndex ~= nil and endIndex < 0 then
endIndex = SubStringGetTotalIndex(str) + endIndex + 1;
end
if endIndex == nil then
return string.sub(str, SubStringGetTrueIndex(str, startIndex));
else
return string.sub(str, SubStringGetTrueIndex(str, startIndex), SubStringGetTrueIndex(str, endIndex + 1) - 1);
end
end
function SubStringGetTotalIndex(str)
local curIndex = 0;
local i = 1;
local lastCount = 1;
repeat
lastCount = SubStringGetByteCount(str, i)
i = i + lastCount;
curIndex = curIndex + 1;
until(lastCount == 0);
return curIndex - 1;
end
function SubStringGetTrueIndex(str, index)
local curIndex = 0;
local i = 1;
local lastCount = 1;
repeat
lastCount = SubStringGetByteCount(str, i)
i = i + lastCount;
curIndex = curIndex + 1;
until(curIndex >= index);
return i - lastCount;
end
function SubStringGetByteCount(str, index)
local curByte = string.byte(str, index)
local byteCount = 1;
if curByte == nil then
byteCount = 0
elseif 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
return byteCount;
end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59