function string.widthSingle(inputstr) -- 计算字符串宽度 -- 可以计算出字符宽度,用于显示使用 local lenInByte = #inputstr local width = 0 local i = 1 while (i<=lenInByte) do local curByte = string.byte(inputstr, i) local byteCount = 1; if curByte>0 and curByte<=127 then byteCount = 1 --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 --4字节字符 end --local char = string.sub(inputstr, i, i+byteCount-1) --print(char) i = i + byteCount -- 重置下一字节的索引 width = width + 1 -- 字符的个数(长度) end return width end function string.ToTable(s) local tb = {} --[[ UTF8的编码规则: 1. 字符的第一个字节范围: 0x00—0x7F(0-127),或者 0xC2—0xF4(194-244); UTF8 是兼容 ascii 的,所以 0~127 就和 ascii 完全一致 2. 0xC0, 0xC1,0xF5—0xFF(192, 193 和 245-255)不会出现在UTF8编码中 3. 0x80—0xBF(128-191)只会出现在第二个及随后的编码中(针对多字节编码,如汉字) ]] for utfChar in string.gmatch(s, "[%z\1-\127\194-\244][\128-\191]*") do table.insert(tb, utfChar) end return tb end --获取中英混合UTF8字符串的真实字符数量 function string.SubStringGetTotalIndex(str) local curIndex = 0; local i = 1; local lastCount = 1; repeat lastCount = string.SubStringGetByteCount(str, i) i = i + lastCount; curIndex = curIndex + 1; until(lastCount == 0); return curIndex - 1; end function string.SubStringGetTrueIndex(str, index) local curIndex = 0; local i = 1; local lastCount = 1; repeat lastCount = string.SubStringGetByteCount(str, i) i = i + lastCount; curIndex = curIndex + 1; until(curIndex >= index); return i - lastCount; end --返回当前字符实际占用的字符数 function string.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 --截取中英混合的UTF8字符串,endIndex可缺省 function string.SubStringUTF8(str, startIndex, endIndex) if startIndex < 0 then startIndex = string.SubStringGetTotalIndex(str) + startIndex + 1; end if endIndex ~= nil and endIndex < 0 then endIndex = string.SubStringGetTotalIndex(str) + endIndex + 1; end if endIndex == nil then return string.sub(str, string.SubStringGetTrueIndex(str, startIndex)); else return string.sub(str, string.SubStringGetTrueIndex(str, startIndex), string.SubStringGetTrueIndex(str, endIndex + 1) - 1); end end function string.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
LuaString
最新推荐文章于 2024-04-19 08:30:00 发布