lua string方法拓展
--[[-- 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 local input = "Hello,World" local res = string.split(input, ",") -- res = {"Hello", "World"} local input = "Hello-+-World-+-Quick" local res = string.split(input, "-+-") -- res = {"Hello", "World", "Quick"} @param string input 输入字符串 @param string delimiter 分割标记字符或字符串 @return array 包含分割结果的数组 ]] function string.split(input, delimiter) input = tostring(input) delimiter = tostring(delimiter) if (delimiter=='') then return false end local pos,arr = 0, {}for st,sp in function() return string.find(input, delimiter, pos, true) end do table.insert(arr, string.sub(input, pos, st - 1)) pos = sp + 1 end table.insert(arr, string.sub(input, pos)) return arr end --[[-- 去除输入字符串头部的空白字符,返回结果 local input = " ABC" print(string.ltrim(input)) -- res ABC,输入字符串前面的两个空格被去掉了 空白字符包括: - 空格 - 制表符 \t - 换行符 \n - 回到行首符 \r @param string input 输入字符串 @return string 结果 @see string.rtrim, string.trim ]] function string.ltrim(input) return string.gsub(input, "^[ \t\n\r]+", "") end
--[[-- 去除输入字符串尾部的空白字符,返回结果 local input = "ABC " print(string.ltrim(input)) -- res ABC,输入字符串最后的两个空格被去掉了
@param string input 输入字符串 @return string 结果 @see string.ltrim, string.trim ]] function string.rtrim(input) return string.gsub(input, "[ \t\n\r]+$", "") end
--[[-- 去掉字符串首尾的空白字符,返回结果 @param string input 输入字符串 @return string 结果 @see string.ltrim, string.rtrim ]] function string.trim(input) input = string.gsub(input, "^[ \t\n\r]+", "") return string.gsub(input, "[ \t\n\r]+$", "") end --[[-- 将字符串的第一个字符转为大写,返回结果 local input = "hello" print(string.ucfirst(input)) -- res Hello @param string input 输入字符串 @return string 结果 ]] function string.ucfirst(input) return string.upper(string.sub(input, 1, 1)) .. string.sub(input, 2) end--[[-- 计算 UTF8 字符串的长度,每一个中文算一个字符 local input = "你好World" print(string.utf8len(input)) -- res 7
@param string input 输入字符串 @return integer 长度 ]] 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
----过滤出规范字符 只保留汉字、数字、字符 function string.filterSpecChars(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 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 elseif c<252 then k = k + 5 elseif c<254 then k = k + 6 end end return table.concat(ss) end
--[[-- 将数值格式化为包含千分位分隔符的字符串 print(string.formatnumberthousands(1924235)) -- 输出 1,924,235 @param number num 数值 @return string 格式化结果 ]] function string.formatnumberthousands(num) local formatted = tostring(checknumber(num)) local k while true do formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2') if k == 0 then break end end return formatted end -- 判断utf8字符byte长度 -- 0xxxxxxx - 1 byte -- 110yxxxx - 192, 2 byte -- 1110yyyy - 225, 3 byte -- 11110zzz - 240, 4 byte local function chsize(char) if not char then print("not char") return 0 elseif char > 240 then return 4 elseif char > 225 then return 3 elseif char > 192 then return 2 else return 1 end end -- 截取utf8 字符串 -- str: 要截取的字符串 -- startChar: 开始字符下标,从1开始 -- numChars: 要截取的字符长度 function string.utf8sub(str, startChar, numChars) local startIndex = 1 while startChar > 1 do local char = string.byte(str, startIndex) startIndex = startIndex + chsize(char) startChar = startChar - 1 end local currentIndex = startIndex while numChars > 0 and currentIndex <= #str do local char = string.byte(str, currentIndex) currentIndex = currentIndex + chsize(char) numChars = numChars -1 end return str:sub(startIndex, currentIndex - 1) end
--判断字符串是否为nil或““ function string.IsNilOrEmpty(str) if not str or str == "" then return true end return false end