Lua语法小贴士(二)string库

来源:互联网 发布:Ubuntu sambaclient 编辑:程序博客网 时间:2024/06/06 15:43

string库

byte方法,返回byte值:

print(string.byte("abc")) --97print(string.byte("abc", 2, 3)) --98 99


char方法,连接char成字符串:

print(string.char(100,101,102)) --def

find方法,查找子字符串的起始和结束下标:

local s = "It's 10 o'clock in the morning."local p = "%d+ o'"print(string.find(s,p)) --6 10print(string.find(s,p,7)) --7 10print(string.find(s,p,1,true)) --nilprint(string.find(s,"o'",1,true)) --9 10

第三个参数代表从第几个字符开始查找,第四个参数表示是否是明文(即不使用变量模式)

match方法,查找子字符串:

local s = "Today is 10/10/2016"local p = "%d+/%d+/%d+"print(string.match(s,p)) --10/10/2016print(string.match(s,p,11)) --0/10/2016

format方法,格式化字符串:

print(string.format("%d,%f,%x,%s",10,7.25,92,"num")) -- 10,7.250000,5c,num

gmach方法,返回匹配模式的遍历:

local s = "Hello world from lua"for w in string.gmatch(s,"%a+") do    print(w)end

len方法,返回长度:

print("") --0print("a\000bc\000") --5
lower方法,转换成小写字母:

print(string.lower("LUA")) --lua
upper方法,转换成大写字母:

print(string.upper("lua")) --LUA


rep方法,重复字符串:

print(string.rep("hia",3)) --hiahiahia
reverse方法,翻转字符串:

print(string.reverse("lemon")) --monel

sub方法,子串:

print(string.sub("function"),5,8) --tion

gsub方法,替换子串,返回替换后的字符串和被替换的次数:

local s = "All over the world"lcoal p = "l"local r = "x"print(string.gsub(s,p,r))  --Axx over the worxd 3print(string.gsub(s,p,r,1)) --Axl over the world 1print(string.gsub(s,p,r,2)) --Axx over the world 2print(string.gsub(s,p,r,3)) --Axx over the worxd 3print(string.gsub(s,p,r,4)) --Axx over the worxd 3

dump方法,将传入的function转换成二进制的形式,这样就可以使用loadstring来获取function的副本。function必须是不包含upvalues的Lua function。
local function DumpTest()    print("Test")endlocal a = string.dump(DumpTest)print(a)local b = loadstring(a)print(b)b()

参考:

Lua语法小贴士(一)字符串基础操作

Lua语法小贴士(二)string库

Lua语法小贴士(三)魔法字符

1 0