|
- -- 处理UTF8中文,显示长度一个中文等于两个英文,编码长度一个中文等于2,3,4个英文,以英文单位计算长度
- local function _analysisUTF8(str)
- local width = 0
- local i = 1
- local UTF8Map = {}
- while (i <= #str) do
- local curByte = string.byte(str, i)
- local byteCount = 1
- if curByte >= 240 then
- byteCount = 4
- elseif curByte > 225 then
- byteCount = 3
- elseif curByte > 192 then
- byteCount = 2
- end
-
- local char = string.sub(str, i, i+byteCount-1)
- if byteCount == 4 then
- char = "□"
- end
- table.insert(UTF8Map, char) --看看这个字是什么
- i = i + byteCount -- 重置下一字节的索引
- if byteCount == 1 then
- width = width + 1 -- 字符的个数(长度)
- else
- width = width + 2
- end
- end
- return width, UTF8Map
- end
-
- local function filter_spec_chars(s)
- local ss = {}
- local k = 1
- while true do
- if k > #s then break end
- local c = string.byte(s,k)
- if not c then break end
- if c<192 then
- --if (c>=48 and c<=57) or (c>= 65 and c<=90) or (c>=97 and c<=122) then
- table.insert(ss, string.char(c))
- --end
- k = k + 1
- elseif c<224 then
- k = k + 2
- table.insert(ss, string.char(63))
- elseif c<240 then
- if c>=228 and c<=233 then
- local c1 = string.byte(s,k+1)
- local c2 = string.byte(s,k+2)
- if c1 and c2 then
- local a1,a2,a3,a4 = 128,191,128,191
- if c == 228 then a1 = 184
- elseif c == 233 then a2,a4 = 190,c1 ~= 190 and 191 or 165
- end
- if c1>=a1 and c1<=a2 and c2>=a3 and c2<=a4 then
- table.insert(ss, string.char(c,c1,c2))
- end
- end
- end
- k = k + 3
- elseif c<248 then
- k = k + 4
- table.insert(ss, string.char(63))
- elseif c<252 then
- k = k + 5
- table.insert(ss, string.char(63))
- elseif c<254 then
- k = k + 6
- table.insert(ss, string.char(63))
- end
- end
- --return 4,table.concat(ss)
- return 4,ss
- end
-
- -- 获取当前字符展示宽度
- function string.getUTF8Width(str)
- local width, _ = _analysisUTF8(str)
- return width
- end
-
- -- 按照关键词把string转化为table
- function string.toTableByString(str, strKey)
- local strTable = {}
- str = tostring(str)
- function test(str)
- if str == "" then return end
- local str1, str2 = "", ""
- local strLen = string.len(str)
- local begNum, endNum = string.find(str, strKey)
- if type(begNum) ~= "number" or type(endNum) ~= "number" then
- str1 = str
- else
- if type(begNum) == "number" and begNum > 1 then
- str1 = string.sub(str, 1, begNum - 1)
- end
-
- if type(endNum) == "number" and endNum < strLen then
- str2 = string.sub(str, endNum + 1, strLen)
- end
- end
- table.insert(strTable, str1)
- test(str2)
- end
- test(str)
- return strTable
- end
-
- function string.getShortName(str,size)
- local width, UTF8Map = _analysisUTF8(str)
- local num,shortName = 0,""
- local max = #UTF8Map--size < max and
- if type(UTF8Map) == "table" and next(UTF8Map) then
- for _,v in ipairs(UTF8Map) do
- if num < size then
- if v then
- shortName = shortName .. v
- num = num + 1
- end
- end
- end
- if max > size then
- shortName = shortName.."..."
- end
- return shortName
- else
- for _,v in ipairs(UTF8Map) do
- if num < size then
- if v then
- shortName = shortName .. v
- num = num + 1
- end
- end
- end
- return shortName
- end
- end
-
- --另一种方式过滤
- function string.getShortName2(str,size)
- local width, UTF8Map = filter_spec_chars(str)
- local num,shortName = 0,""
- local max = #UTF8Map
- if size < max and type(UTF8Map) == "table" and next(UTF8Map) then
- for _,v in ipairs(UTF8Map) do
- if num < size then
- if v then
- shortName = shortName .. v
- num = num + 1
- end
- end
- end
- if max > size then
- shortName = shortName.."..."
- end
- return shortName
- else
- for _,v in ipairs(UTF8Map) do
- if num < size then
- if v then
- shortName = shortName .. v
- num = num + 1
- end
- end
- end
- return shortName
- end
- end
-
- --[[
- 逆序截取字符串
- ]]
- function string.subStringReverse(str, k)
- local ts = string.reverse(str)
- local _,i = string.find(ts, k)
- local m = string.len(ts) - i + 1
- return string.sub(str, 1, m) --返回字符串str字符k之前的部分
- end
|