本文共 1172 字,大约阅读时间需要 3 分钟。
01.print(string.find("Hellow world","l",4 )) --44
截取字符串
info = "我是小菜鸟"local str = string.sub(info,index,endIndex)-- info 字符串信息--index 开始截取的字的索引--endIndex 结束时的索引 print(string.sub(info,1,6))--我是print(string.sub("Hellowworld",1,3 )) --Hel
print(string.find( "5898{5wd","{",1,true )) --5 5
print(string.gsub("5898{5wd","wd" ,"666"))04.截取字符串为表
function string.split(input, delimiter) if not input or string.len(input) == 0 or not delimiter or string.len(delimiter) == 0 then return { } end input = tostring(input) delimiter = tostring(delimiter) 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 arrendlocal tab=string.split("dfd[pf[gpl","[")for i=1,#tab do print(tab[i])end
05.string.gmatch
local str = "123=sd456=,ff,45="local str1 = string.gmatch(str, "(%d+)") print(str1()) -- 123print(str1()) -- 456print(str1()) -- 45local s="some-s-tring-" local words={ }for w in string.gmatch( s,"(%a+)-") do print(w)endsomestring
转载地址:http://hfrxo.baihongyu.com/