miduo_client/Assets/ManagedResources/~Lua/UITools/StringHelper.lua

74 lines
2.0 KiB
Lua
Raw Normal View History

2020-05-09 13:31:21 +08:00
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- DateTime: 2018/5/7 0007 上午 10:53
--- 字符串帮助器
--矫正名字显示长度
function FixNameShow(playerName)
local trueLen = SubStringGetTotalIndex(playerName)
if trueLen>6 then
playerName = SubStringUTF8(playerName,1,5)
playerName = playerName..'...'
end
return playerName
end
--截取中英混合的UTF8字符串endIndex可缺省
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
--获取中英混合UTF8字符串的真实字符数量
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