您的位置:首页 > 编程语言 > Lua

Lua学习笔记Day1-Lua标准库函数

2016-05-16 21:56 471 查看

Lua学习笔记Day1-Lua标准库函数

Lua学习笔记Day1-Lua标准库函数
目录

Math库

String库

Table库

目录

Math库

String库

Table库

本文内容来自Lua官方文档

Math库

math.pi 返回pi的值、
math.huge 返回一个最大数
math.abs(x) 返回x的绝对值
math.ceil(x) 向上取整
math.floor(x) 向下取整

math.fmod(x,y) x/y取模,注意小数
math.modf (x) 取x的整数和小数部分
math.sqrt(x) 返回x的开平方
math.pow(x,y) 返回x的y次幂
math.exp(x) 返回e的x次幂
math.frexp(m) m = x*(2^y),把m拆成x和y两部分
math.ldexp(x,y) 返回x*(2^y)

math.random(x,y) 没有参数返回[0,1]之间的数,一个整数参数m返回[1,m]之间的整数
math.randomseed(x) 设置随机数种子
os.time() 返回当前系统时间距离1970年1月1日0秒的秒数,可以传一个参数
os.date() 返回本地化的时间字符串

math.deg(x) 弧度转角度
math.rad(x) 角度转弧度
math.cos(x) 返回x的余弦值,结果是弧度
math.cosh(x) 返回双曲线余弦值
math.sin(x) 返回x的正弦值,结果是弧度
math.sinh(x) 返回双曲线正弦值
math.acos(x) 返回x的反余弦值,结果是弧度
math.asin(x) 返回x的反正弦值,结果是弧度
math.atan(x) 返回x的反正切值,结果是弧度
math.atan(y,x) 返回y/x的反正切值,结果是弧度,x是0也会正确处理


String库

string.len(str) 返回str的长度
string.lower(str) string.upper(str) 把str转化为小写|大写
string.byte() str:byte(1,3) 返回字符串str中从第1位到第3位的ASCII值
string.char() string:char(49,50,51) 返回ASCII值对应的字符串
string.dump(function) 返回指定函数的二进制代码(函数必须是一个Lua函数,并且没有上值)

string.find(s, pattern [,init [,plain]])
string.find(s,"abcd",1,plain) 查找s中首次出现"ab"的位置,从1位置开始,如果找到则返回首次出现的起始和结束位置(1,4)否则返回nil

string.format(formatstring,...) 格式化字符串
string.format('%s', 'a string with "quotes" and \n new \t line')
结果:
a string with "quotes" and
new      line
%q:为自动将对应参数字串中的特殊字符加上\

string.match(s,pattern,init) 查找s中从init开始是否有pattern,如果有则返回pattern
string.gmatch(s,pattern) 返回一个迭代函数,每次调用此函数,将返回下一个查找到的样式串对应的字符
s = "hello world from Lua"
for w in string.gmatch(s, "%a+") do
print(w)
end
结果:
hello
world
from
Lua

string.sub(s, i [,j]) 字符串截取i位置和j位置之间的字符串
string.gsub(s, pat, repl [,n]) 将字符串中的pat替换为repl,返回字符串及替换的次数
string.rep(s, n) 把字符串s重复n次
string.reverse(s) 返回s的反转


Table库

table.concat(table,sep,start,end) 如果一个表里都是字符串或数字,用concat可以把它们连接起来,sep是分割符
table.insert(table, [pos,]value) 把value插入到table的pos位置,pos为空表示尾部插入
table.remove(table[,pos]) 移除table表pos位置的元素
table.sort(table [,comp]) table排序,默认顺序是数字从小到大,字母abc...
table.sort(network,function(a,b) return(a.name < b.name) end)
table.maxn(table) 返回table的最大索引
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lua 库函数