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中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言
- Lua中获取table长度问题探讨