string
1、反转 reverse(s)---print(string.reverse('hello lua'))---aul olleh
2、替换子字符串 gsub(s,p,r[,n])---print(string.gsub("lua lua lua","lua","hello",2)) -- lua hello hello
3、截取子字符串 sub(s,i[,j]) -- print(string.sub("lualua",2,4) -- ual
4、返回字符串的n次拷贝(克隆) rep(s,n) -- print(string.rep("lua",3)) -- lualualua
5、通过迭代器函数,遍历满足要求的字符串 gmatch(s,p)--
local s = "hello world form lua"
for w in string.gmatch(s,"%a+") do
print(w)
end
结果:
hello
wrold
from
lua
6、匹配满足要求的子字符串 (返回子字符串或者nil)
match(s,p[,init])--
print(string.match("lua lua","lua")) -- lua
print(string.match("lua lua","lua",3)) -- lua
print(string.match("lua lua","hello") -- nil
print(string.match("lua lua","lua",-2) -- string.len("lua lua")-2 == 5 -- nil
7、参照格式化参数格式化文本
format(formatstring,....)
--print(string.format("%.4f",3.1415987)) -- 3.1416
8、匹配满足条件的子字符串 (返回子字符串在主字符串中的开始,结束索引或者nil)
print(string.find("lua lua","lua")) -- 1 3
print(string.find("lua lua","lua",5)) -- nil