Skip to content

常用类库

Lua语言自带了一组基础函数和工具库,它们为Lua编程提供了丰富的功能和便利的操作接口。以下是Lua语言自带的常用函数库,其中包括了字符串处理、表操作、数学计算、文件I/O、操作系统交互等多个方面。

字符串处理库(string)

提供了一系列用于处理字符串的函数,如查找、替换、格式化等。

函数功能
string.len(s)返回字符串s的长度
string.rep(s, n)将字符串s重复n次并返回结果字符串
string.lower(s)将字符串s中的所有字符转换为小写并返回
string.upper(s)将字符串s中的所有字符转换为大写并返回
string.sub(s, i, [j])截取字符串s中从索引i到索引j(包含ij)之间的子串。
索引从1开始,可以为负数,其中-1表示最后一个字符
string.char(...)将一系列符合ASCII值的整数转化为相对应的字符
string.byte(s, [i, [j]])将字符串s中索引i到索引j(包含ij)之间的字符转换为对应的ASCII值
如果只提供一个索引,则返回该索引处字符的ASCII值
string.format(formatstring, ...)用于格式化字符串。formatstring指定格式,后续参数按照该格式进行格式化
string.find(s, p, [init, [plain]])s 字符串中从索引init开始匹配p(模式)字符串
若匹配成功,则返回 p 字符串在 s 字符串中出现的开始位置和结束位置
若匹配失败,则返回 nil
如果plaintrue,则p被视为普通字符串而非模式
string.match(s, p, [init])s 字符串中从索引init开始匹配p(模式)字符串
若匹配成功,则返回目标字符串中与模式匹配的子串;否则返回 nil
string.gsub(s, p, r, [n])s 字符串中匹配p(模式)字符串,并用r替换
n参数指定了最多替换的次数,默认为全部替换
返回替换后的字符串和替换次数
string.gmatch(s, p)返回一个迭代器,该迭代器遍历字符串s中所有符合p(模式)的子串

使用示例

lua
----string.len(s)
print(string.len("Hello")) 									--输出:5

----string.rep(s, n)
print(string.rep("abc", 3)) 								--输出:abcabcabc

----string.lower(s)
print(string.lower("Hello World")) 							--输出:hello world

----string.upper(s)
print(string.upper("Hello World")) 							--输出:HELLO WORLD

----string.sub(s, i, [j])
print(string.sub("Hello", 2, 4)) 							--输出:ell

----string.char(...)
print(string.char(97, 98, 99)) 								--输出:abc

----string.format(formatstring, ...)
print(string.format("%d %s", 123, "Hello")) 				--输出:123 Hello

----string.find(s, p, [init, [plain]])
print(string.find("Hello World", "World"))  				--输出:7	11

----string.match(s, p, [init])
print(string.match("Hello World", "World")) 				--输出:World

----string.gsub(s, p, r, [n])
print(string.gsub("Hello World", "World", "Lua")) 			--输出:Hello Lua	1

----string.gmatch(s, p)
s = "hello world from Lua"
--匹配最长连续且只含字母的字符串
for w in string.gmatch(s, "%a+") do
    print(w)
end	
--[[
输出:
hello
world
from
Lua
--]]
----string.len(s)
print(string.len("Hello")) 									--输出:5

----string.rep(s, n)
print(string.rep("abc", 3)) 								--输出:abcabcabc

----string.lower(s)
print(string.lower("Hello World")) 							--输出:hello world

----string.upper(s)
print(string.upper("Hello World")) 							--输出:HELLO WORLD

----string.sub(s, i, [j])
print(string.sub("Hello", 2, 4)) 							--输出:ell

----string.char(...)
print(string.char(97, 98, 99)) 								--输出:abc

----string.format(formatstring, ...)
print(string.format("%d %s", 123, "Hello")) 				--输出:123 Hello

----string.find(s, p, [init, [plain]])
print(string.find("Hello World", "World"))  				--输出:7	11

----string.match(s, p, [init])
print(string.match("Hello World", "World")) 				--输出:World

----string.gsub(s, p, r, [n])
print(string.gsub("Hello World", "World", "Lua")) 			--输出:Hello Lua	1

----string.gmatch(s, p)
s = "hello world from Lua"
--匹配最长连续且只含字母的字符串
for w in string.gmatch(s, "%a+") do
    print(w)
end	
--[[
输出:
hello
world
from
Lua
--]]

表处理库(table)

提供了一系列用于操作Lua表的函数,如表的插入、删除、排序等。

函数功能
table.insert(table, [pos,] value)在表table的指定位置pos(默认为表的末尾)插入一个元素value
table.remove(table, [pos])从表table中移除指定位置pos(默认为表的末尾)的元素,并返回该元素
table.concat(table, [sep[, start[, end]]])连接表table的数组部分,生成一个以sep分隔字符串
sep默认为空字符串
start默认为1
end默认为table的长度
table.sort(table, [comp])对表table的数组部分进行排序
comp为自定义排序规则的比较函数,不提供则按升序排序

使用示例

lua
----table.insert(table, [pos,] value)
t = {1, 2, 3}  
table.insert(t, 2, 4)  -- 结果:t = {1, 4, 2, 3}  
table.insert(t, "a")   -- 默认插入到末尾,结果:t = {1, 4, 2, 3, "a"}

----table.remove(table, [pos])
t = {1, 2, 3, 4}  
table.remove(t, 2)  -- 移除位置2的元素,返回2,结果:t = {1, 3, 4}  
table.remove(t)     -- 移除最后一个元素,返回4,结果:t = {1, 3}

----table.concat(table, [sep[, start[, end]]])
t = {"Lua", "C++", "Python"}  
print(table.concat(t, ", "))  -- 输出:Lua, C++, Python  
print(table.concat(t, ", ", 2, 3))  -- 输出:C++, Python

----table.sort(table, [comp])
t = {4, 1, 3, 5, 2}  
table.sort(t)  -- 按升序排序,结果:t = {1, 2, 3, 4, 5}  
table.sort(t, function(a, b) return a > b end)  -- 按降序排序
----table.insert(table, [pos,] value)
t = {1, 2, 3}  
table.insert(t, 2, 4)  -- 结果:t = {1, 4, 2, 3}  
table.insert(t, "a")   -- 默认插入到末尾,结果:t = {1, 4, 2, 3, "a"}

----table.remove(table, [pos])
t = {1, 2, 3, 4}  
table.remove(t, 2)  -- 移除位置2的元素,返回2,结果:t = {1, 3, 4}  
table.remove(t)     -- 移除最后一个元素,返回4,结果:t = {1, 3}

----table.concat(table, [sep[, start[, end]]])
t = {"Lua", "C++", "Python"}  
print(table.concat(t, ", "))  -- 输出:Lua, C++, Python  
print(table.concat(t, ", ", 2, 3))  -- 输出:C++, Python

----table.sort(table, [comp])
t = {4, 1, 3, 5, 2}  
table.sort(t)  -- 按升序排序,结果:t = {1, 2, 3, 4, 5}  
table.sort(t, function(a, b) return a > b end)  -- 按降序排序

数学库(math)

提供了一系列数学函数,如三角函数、指数函数、对数函数等。

函数功能
math.abs(x)返回x的绝对值
math.ceil(x)返回最小且不小于x的整数
math.floor(x)返回最大且不大于x的整数
math.max(x, ...)返回参数中值最大的那个数,参数必须是number型
math.min(x, ...)返回参数中值最小的那个数,参数必须是number型
math.fmod(x, y)返回 x对y取余数
math.pow(x, y)返回x的y次方
math.sqrt(x)返回x的算术平方根
math.exp(x)返回自然数e的x次方
math.pi圆周率
math.sin(x)求弧度x的正弦值
math.cos(x)求弧度x的余弦值
math.tan(x)求弧度x的正切值
math.asin(x)求x的反正弦值
math.acos(x)求x的反余弦值
math.atan(x)求x的反正切值
math.random ([m [, n]])不传入参数时,返回 一个在区间[0,1)内均匀分布的伪随机实数;只使用一个整数参数m时,返回一个在区间[1, m]内均匀分布的伪随机整数;使用两个整数参数时,返回一个在区间[m, n]内均匀分布的伪随机整数
math.randomseed (x)为伪随机数生成器设置一个种子x,相同的种子将会生成相同的数字序列

使用示例

lua
print(math.abs(-5))  -- 输出: 5

print(math.ceil(3.4))  -- 输出: 4  
print(math.ceil(-3.2))  -- 输出: -3

print(math.ceil(3.4))  -- 输出: 4  
print(math.ceil(-3.2))  -- 输出: -3

print(math.max(2.71, 100, -98, 23))  -- 输出: 100  

print(math.min(2.71, 100, -98, 23))  -- 输出: -98

print(math.fmod(10.1, 3))   --输出: 1.1

print(math.pow(2, 3))   --输出: 8.0

print(math.sqrt(25))   --输出: 5.0

print(math.exp(1))   --输出: 2.718281828459

print(math.pi)   --输出: 3.1415926535898

-- 使用math.rad将角度转换为弧度 
print(math.sin(math.rad(30)))   --输出: 0.5

print(math.cos(math.rad(30)))   --输出: 0.86602540378444

print(math.tan(math.rad(30)))   --输出: 0.57735026918963

print(math.acos(math.cos(math.rad(30))))   --输出: 0.5235987755983

print(math.asin(math.sin(math.rad(30))))   --输出: 0.5235987755983

print(math.atan(math.tan(math.rad(30))))   --输出: 0.5235987755983

math.randomseed(os.time())  -- 设置随机种子

print(math.random())  -- 输出一个[0,1)内的浮点数
print(math.random(10))  -- 输出1到10之间的一个整数
print(math.random(3, 5))  -- 输出3到5之间的一个整数
print(math.abs(-5))  -- 输出: 5

print(math.ceil(3.4))  -- 输出: 4  
print(math.ceil(-3.2))  -- 输出: -3

print(math.ceil(3.4))  -- 输出: 4  
print(math.ceil(-3.2))  -- 输出: -3

print(math.max(2.71, 100, -98, 23))  -- 输出: 100  

print(math.min(2.71, 100, -98, 23))  -- 输出: -98

print(math.fmod(10.1, 3))   --输出: 1.1

print(math.pow(2, 3))   --输出: 8.0

print(math.sqrt(25))   --输出: 5.0

print(math.exp(1))   --输出: 2.718281828459

print(math.pi)   --输出: 3.1415926535898

-- 使用math.rad将角度转换为弧度 
print(math.sin(math.rad(30)))   --输出: 0.5

print(math.cos(math.rad(30)))   --输出: 0.86602540378444

print(math.tan(math.rad(30)))   --输出: 0.57735026918963

print(math.acos(math.cos(math.rad(30))))   --输出: 0.5235987755983

print(math.asin(math.sin(math.rad(30))))   --输出: 0.5235987755983

print(math.atan(math.tan(math.rad(30))))   --输出: 0.5235987755983

math.randomseed(os.time())  -- 设置随机种子

print(math.random())  -- 输出一个[0,1)内的浮点数
print(math.random(10))  -- 输出1到10之间的一个整数
print(math.random(3, 5))  -- 输出3到5之间的一个整数

I/O库(io)

提供了一系列用于文件输入输出的函数。

函数说明
io.open(filename [, mode])打开指定文件,并返回文件对象
io.close(file)关闭文件对象
io.read(file [, format])读取文件中的数据,并返回
io.write(file, ...)向文件中写入数据

操作系统库(os)

提供了一系列与操作系统交互的函数,如获取当前时间、执行系统命令等。

函数说明
os.time([table])返回指定时间的时间戳,可选参数table指定时间
os.difftime(t2, t1)返回时间戳t2减去t1的时间差,单位为秒
os.execute(command)在操作系统中执行指定的命令
os.remove(filename)删除指定文件
os.rename(oldname, newname)将指定文件重命名为新名称