lua函数大全
Lua函数库包含了很多有用的函数,以下是一部分常用的函数:
基础函数
print(...)
输出函数,可输出任意个参数。
type
type(v)
返回参数的类型。
tonumber
tonumber(e [, base])
将参数转换为数字类型。
tostring
tostring(e)
将参数转换为字符串类型。
assert
assert(v [, message])
如果v为false或者nil,抛出错误信息。
select
select(index, ...)
返回参数列表中第index个参数及其之后的所有参数。
error
error(message [, level])
抛出指定错误信息。
pcall
pcall(f [, arg1, ...])
调用函数f,并返回是否调用成功以及函数的返回值。
xpcall
xpcall(f, msgh [, arg1, ...])
调用函数f,在出错时调用msgh函数处理错误。
ipairs
ipairs(t)
遍历数组类型的表。
pairs
pairs(t)
遍历非数组类型的表。
next
next(table [, index])
返回表中的下一个键值对。
rawequal
rawequal(v1, v2)
比较两个值是否相等,不调用元方法。
rawget
rawget(table, index)
返回表中指定索引的值,不调用元方法。
rawset
rawset(table, index, value)
将值赋予表中指定索引的位置,不调用元方法。
setmetatable
setmetatable(table, metatable)
设置表的元表。
getmetatable
getmetatable(object)
返回对象的元表。
tostring
tostring(e)
将参数转换为字符串类型。
type
type(v)
返回参数的类型。
unpack
unpack(list [, i [, j]])
返回列表中指定索引范围的值。
字符串函数
string.byte
string.byte(s [, i [, j]])
返回字符串中指定索引范围内的字符的ASCII码。
string.char
string.char(...)
将一组ASCII码转换为字符串。
string.sub
string.sub(s, i [, j])
返回字符串中指定索引范围内的子串。
string.rep
string.rep(s, n [, sep])
返回一个字符串的n个拷贝,可指定拷贝间的分隔符。
string.reverse
string.reverse(s)
返回字符串的反转字符串。
string.format
string.format(formatstring, ...)
按照格式化字符串的规则返回格式化后的字符串。
string.len
string.len(s)
返回字符串的长度。
string.lower
string.lower(s)
返回字符串的小写字符串。
string.upper
string.upper(s)
返回字符串的大写字符串。
string.gsub
string.gsub(s, pattern, replace [, n])
将字符串中符合正则表达式的子串替换为指定的字符串。
string.find
string.find(s, pattern [, init [, plain]])
在字符串中查找符合正则表达式的子串。
string.match
string.match(s, pattern [, init])
在字符串中查找符合正则表达式的子串,并返回结果。
string.gmatch
string.gmatch(s, pattern)
返回一个迭代器,用于遍历字符串中的符合正则表达式的所有子串。
string.gsub
string.gsub(s, pattern, replace [, n])
将字符串中符合正则表达式的子串替换为指定的字符串。
数学函数
math.abs
math.abs(x)
返回x的绝对值。
math.acos
math.acos(x)
返回x的反余弦值,单位为弧度。
math.asin
math.asin(x)
返回x的反正弦值,单位为弧度。
math.atan
math.atan(x)
返回x的反正切值,单位为弧度。
math.atan2
math.atan2(y, x)
返回y/x的反正切值,单位为弧度。
math.ceil
math.ceil(x)
返回x的向上取整值。
math.cos
math.cos(x)
返回x的余弦值,单位为弧度。
math.cosh
math.cosh(x)
返回x的双曲余弦值。
math.deg
math.deg(x)
将弧度转换为角度。
math.exp
math.exp(x)
返回e的x次方。
math.floor
math.floor(x)
返回x的向下取整值。
math.fmod
math.fmod(x, y)
返回x除以y的余数。
math.frexp
math.frexp(x)
返回x的尾数和指数。
math.huge
math.huge
表示正无穷大的常量。
math.ldexp
math.ldexp(m, e)
返回m*2^e的结果。
math.log
math.log(x [, base])
返回x的对数。
math.log10
math.log10(x)
返回x以10为底的对数。
math.max
math.max(x, ...)
返回参数中的最大值。
math.min
math.min(x, ...)
返回参数中的最小值。
math.modf
math.modf(x)
返回x的整数部分和小数部分。
math.pi
math.pi
表示圆周率的常量。
math.pow
math.pow(x, y)
返回x的y次方。
math.rad
math.rad(x)
将角度转换为弧度。
math.random
math.random([m [, n]])
返回[m, n]之间的随机数,如果不传参数,则返回[0,1]之间的随机数。
math.randomseed
math.randomseed(x)
设置随机数种子。
math.sin
math.sin(x)
返回x的正弦值,单位为弧度。
math.sinh
math.sinh(x)
返回x的双曲正弦值。
math.sqrt
math.sqrt(x)
返回x的平方根。
math.tan
math.tan(x)
返回x的正切值,单位为弧度。
math.tanh
math.tanh(x)
返回x的双曲正切值。
表函数
table.concat
table.concat(list [, sep [, i [, j]]])
将列表中指定索引范围内的值以指定分隔符连接起来。
table.insert
table.insert(list [, pos,] value)
将值插入到列表中指定位置。
table.maxn
table.maxn(table)
返回列表中最大的数字索引。
table.remove
table.remove(list [, pos])
从列表中删除指定位置的值,并返回该值。
table.sort
table.sort(list [, comp])
对列表进行排序。
原文地址: https://www.cveoy.top/t/topic/byP6 著作权归作者所有。请勿转载和采集!