模式匹配_字符串


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

模式匹配_字符串_02

print(string.find( "5898{5wd","{",1,true )) --5 5



模式匹配_字符串_03



模式匹配_字符串_04

print(string.gsub("5898{5wd","wd" ,"666"))

模式匹配_字符串_05


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

模式匹配_字符串_06

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

模式匹配_字符串_07