模式匹配
原创
©著作权归作者所有:来自51CTO博客作者我的流浪国的原创作品,请联系作者获取转载授权,否则将追究法律责任
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 arr
end
local 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()) -- 123
print(str1()) -- 456
print(str1()) -- 45
local s="some-s-tring-"
local words=
{
}
for w in string.gmatch( s,"(%a+)-") do
print(w)
end
some
s
tring