123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- string.empty = ''
- function string.contains(str, subStr)
- return string.find(str, subStr) ~= nil
- end
- function string.endsWith(str, subStr)
- local len1 = #str
- local len2 = #subStr
- if len1 < len2 then
- return false
- end
- return string.find(str, subStr, len1 - len2 + 1) ~= nil
- end
- function string.isNullOrEmpty(str)
- return str == nil or str == ""
- end
- function string.replace(str, oldStr, newStr)
- local i, j = string.find(str, oldStr, 1, true)
- if i and j then
- ---@class ret
- local ret = {}
- local start = 1
- while i and j do
- table.insert(ret, string.sub(str, start, i - 1))
- table.insert(ret, newStr)
- start = j + 1
- i, j = string.find(str, oldStr, start, true)
- end
- table.insert(ret, string.sub(str, start))
- return table.concat(ret)
- end
- return str
- end
- function string.split(str, sep)
- sep = sep or " "
- local items = {}
- local pattern = string.format("([^%s]+)", sep)
- string.gsub(str, pattern, function (c)
- table.insert(items, c) end)
- return items
- end
- ---@param pattern string @例如 '<.->',里面的减号表示尽可能少,例如 abc<fdsafdsa>df<dddd> 分割后返回 abc <fdsafdsa> df <dddd>
- ---但是 abc<fdd<d>d> 会匹配成 abc <fdd<d> d>
- function string.splitPair(str,pattern)
- local items = {}
- --或者 %b<>
- local temp = {}
- for v in string.gmatch(str,pattern) do
- table.insert(temp,v)
- end
- local index = 1
- local tempIndex = 1
- local length = string.len(str)
- while index <= length and tempIndex<=#temp and not string.isNullOrEmpty(str) do
- local i = string.indexOf(str,temp[tempIndex])
- local pre = string.sub(str,1,i-1)
- if not string.isNullOrEmpty(pre) then
- table.insert(items,pre)
- end
- table.insert(items,temp[tempIndex])
- index = i+string.len(temp[tempIndex])
- str = string.sub(str,index)
- tempIndex = tempIndex+1
- index = i+1
- end
- if not string.isNullOrEmpty(str) then
- table.insert(items,str)
- end
- return items
- end
- ---@return table @abc<fdd<d>d> 会匹配成 abc <fdd<d>d> 完全匹配 left:'<' right:'>'
- function string.splitCompletePairMatch(str,left,right)
- local items = {}
- local index = 1
- local length = string.len(str)
- local leftCount = 0
- local preIndex = 1
- while index<=length do
- local c = string.sub(str,index,index)
- if c == left then
- if leftCount==0 then
- if preIndex<=index-1 then
- local pre = string.sub(str,preIndex,index-1)
- preIndex = index
- if not string.isNullOrEmpty(pre) then
- table.insert(items,pre)
- end
- end
- end
- leftCount = leftCount + 1
- elseif c == right then
- if leftCount~=0 then
- leftCount = leftCount - 1
- if leftCount==0 then
- local p = string.sub(str,preIndex,index)
- preIndex = index+1
- table.insert(items,p)
- end
- else
- local p = string.sub(str,preIndex,index)
- preIndex = index+1
- table.insert(items,p)
- end
- end
- index = index+1
- end
- if preIndex<=length then
- local p = string.sub(str,preIndex,length)
- table.insert(items,p)
- end
- return items
- end
- ---@return string[] @例如 分隔符/,111/aaa 分割之后是 111 /aaa
- function string.splitToArray(str,sep)
- local items = string.splitPair(str,sep)
- local t = {}
- local s=string.empty
- for k, v in pairs(items) do
- if v == sep then
- s = v
- elseif not string.isNullOrEmpty(s) then
- table.insert(t,s..v)
- s = string.empty
- else
- table.insert(t,v)
- end
- end
- return t
- end
- ---@return string[] @例如 helloWorld 分隔符llo 分割之后是he World
- function string.splitByAll(str, sep)
- if type(sep) ~= "string" or #sep <= 0 then
- return
- end
- local index = 1
- local strTb = {}
- while true do
- local pos = string.find(str, sep, index, true)
- if not pos then
- break
- end
- table.insert (strTb, string.sub (str, index, pos - 1))
- index = pos + string.len (sep)
- end
- table.insert (strTb, string.sub (str, index))
- return strTb
- end
- function string.getChar(str,index)
- return string.sub(str,index,index)
- end
- function string.startsWith(str, subStr)
- return string.find(str, subStr) == 1
- end
- local EMPTY_CHARS = " \t\n\r"
- function string.trim(str, chars)
- chars = chars or EMPTY_CHARS
- return str:match(string.format("^[%s]*(.-)[%s]*$", chars, chars))
- end
- function string.trimLeft(str, chars)
- chars = chars or EMPTY_CHARS
- return str:match(string.format("^[%s]*(.*)", chars))
- end
- function string.trimRight(str, chars)
- chars = chars or EMPTY_CHARS
- return str:match(string.format("(.-)[%s]*$", chars))
- end
- function string.indexOf(s, pattern, init)
- init = init or 0
- local index = string.find(s, pattern, init, true)
- return index or -1;
- end
- function string.lastIndexOf(s, pattern)
- local i=s:match(".*"..pattern.."()")
- if i==nil then
- return nil
- else
- return i-1
- end
- end
- ---@return string @顺序匹配是否包含
- function string.containsByOrder(str,subStr)
- local len = string.len(str)
- local subLen = string.len(subStr)
- local lenIndex = 1
- local subLenIndex = 1
- while subLenIndex<=subLen and lenIndex<=len do
- local subChar = string.getChar(subStr,subLenIndex)
- local char = string.getChar(str,lenIndex)
- if subChar == char then
- subLenIndex = subLenIndex+1
- lenIndex = 1
- else
- lenIndex = lenIndex+1
- end
- end
- if subLenIndex>=subLen then
- return true
- end
- return false
- end
- function string.insert(str,index,insertStr)
- local pre = string.sub(str, 1, index -1)
- local tail = string.sub(str, index, -1)
- local createStr = string.format("%s%s%s", pre, insertStr, tail)
- return createStr
- end
- --获取文件名
- function string.getFileName(filename)
- return string.match(filename, ".+/([^/]*%.%w+)$")
- end
- function string.getFileNameWithoutExtension(filename)
- return string.match(filename, ".+/([^/]*)%.%w+$")
- end
- --获取扩展名
- function string.getExtension(str)
- return str:match(".+%.(%w+)$")
- end
- ---@return string @减少无效匹配的gc分配
- function string.gsub_opti(s, pattern, repl, n)
- if string.contains(s,pattern) then
- return string.gsub(s, pattern, repl, n)
- else
- return s
- end
- end
- ---@return string @减少无效匹配的gc分配
- function string.gmatch_opti(s, pattern)
- if string.contains(s,pattern) then
- return string.gmatch(s, pattern)
- else
- return nil
- end
- end
|