--==============================-- -- 字符串相关扩展方法 --==============================-- local tonumber = tonumber local next = next local pairs = pairs local string = string local table = table local math = math local stringLen = string.len local stringByte = string.byte local stringSub = string.sub local stringGsub = string.gsub local stringFind = string.find local stringMatch = string.match local tableInsert = table.insert local mathFloor = math.floor --==============================-- --desc: 通过utf8获取字符串长度 --@str: 字符串 --@return 字符串长度 --==============================-- function string.Utf8Len(str) local len = stringLen(str) local left = len local cnt = 0 local arr = { 0, 192, 224, 240, 248, 252 } while left ~= 0 do local tmp = stringByte(str, -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 --==============================-- --desc: 通过utf8获取单个字符长度 --@str: 单个字符 --@return 字符串长度 --==============================-- function string.Utf8Size(char) if not char then return 0 elseif char >= 252 then return 6 elseif char >= 248 then return 5 elseif char >= 240 then return 4 elseif char >= 225 then return 3 elseif char >= 192 then return 2 else return 1 end end --==============================-- --desc: 按utf8长度截取字符串 --@str: 字符串 --@return 字符串 --==============================-- function string.Utf8Sub(str, startChar, numChars) local startIndex = 1 while startChar > 1 do local char = stringByte(str, startIndex) startIndex = startIndex + string.Utf8Size(char) startChar = startChar - 1 end local currentIndex = startIndex while numChars > 0 and currentIndex <= #str do local char = stringByte(str, currentIndex) currentIndex = currentIndex + string.Utf8Size(char) numChars = numChars - 1 end return str:sub(startIndex, currentIndex - 1) end --==============================-- --desc: 将字符串分割成char table --@str: 字符串 --@return char table --==============================-- function string.SplitWordsToCharTab(str) local len = stringLen(str) local left = len local chartab = {} local arr = { 0, 192, 224, 240, 248, 252 } while left ~= 0 do local tmp = stringByte(str, -left) local i = #arr local value = left while arr[i] do if tmp >= arr[i] then left = left - i break end i = i - 1 end local char = stringSub(str, -value, -left - 1) if stringSub(str, -left, -left + 1) == "\\n" then left = left - 2 char = char .. "\n" end tableInsert(chartab, char) end return chartab end --==============================-- --desc: 把有富文本格式的字符串变成char table --@str: 富文本字符串 --@return char table --==============================-- function string.CharsConvertToCharTab(str) --str = "我只是用来{测一测}别xiabibi{红色试一试}试玩啦"-- local leftindexs = {} local rightindexs = {} local startpos = 1 while true do local pos = stringFind(str, "{", startpos) if not pos then break end tableInsert(leftindexs, pos) pos = stringFind(str, "}", pos + 1) if not pos then break end tableInsert(rightindexs, pos) startpos = pos + 1 end local words = {} if #leftindexs > 0 then startpos = 1 for i = 1, #leftindexs do tableInsert(words, stringSub(str, startpos, leftindexs[i] - 1)) tableInsert(words, stringSub(str, leftindexs[i] + 1, rightindexs[i] - 1)) startpos = rightindexs[i] + 1 end if rightindexs[#rightindexs] ~= stringLen(str) then tableInsert(words, stringSub(str, startpos)) end else tableInsert(words, str) end local result = {} for i = 1, #words do local tab local IsRichText local format if stringSub(words[i], 1, 1) == "<" then IsRichText = true local pa = stringMatch(words[i], "%b>(.*)<") format = stringGsub(words[i], "%b>#$#\|-_=+*()!@#$%^&*~` ]] local FilterSymbolsTable = FilterSymbols:SplitWordsToCharTab() function string.FilterWords(str) local result = "" for i = 1, string.Utf8Len(str) do local nowStr = string.Utf8Sub(str, i, 1) local isValid = true for _, v in pairs(FilterSymbolsTable) do if nowStr == v then isValid = false break end end if isValid then result = result .. nowStr end end return result end --==============================-- --desc: 字符串每隔X个字符插入 需要的字符串 --@str: 字符串对象 --@interval: 间隔多少个字符 --@insertStr: 插入的字符串 --@return 过滤后文本 --==============================-- function string.InsertStr(str, interval, insertStr) local index = 1 local worldCount = 0 local result = "" while true do local world = stringSub(str, index, index) local byte = stringByte(world) if byte > 128 then world = stringSub(str, index, index + 2) index = index + 3 else index = index + 1 end result = result .. world worldCount = worldCount + 1 if worldCount >= interval then result = result .. insertStr worldCount = 0 end if index > #str then break end end return result end --判断字符串是否为合法IP"[0-255].[0-255].[0-255].[0-255]" function string.IsIp(ip) if string.IsNilOrEmpty(ip) then return false end local valueSet = table.pack(string.find(ip, "(%d+)%.(%d+)%.(%d+)%.(%d+)")) if not valueSet[1] then return false end local ipNum for i = 3, 6 do ipNum = tonumber(valueSet[i]) if not ipNum or ipNum < 0 or ipNum > 255 then return false end end return true end