参考:
http://www.cnblogs.com/superchao8/archive/2011/06/20/2085023.html
http://blog.csdn.net/goodai007/article/details/8076141
http://www.cnblogs.com/whiteyun/archive/2009/08/10/1543139.html
0.使用Sublime Text3 配置Lua运行环境:
http://blog.csdn.net/albertsh/article/details/52297546
1.string
--string库中所有的function都不会直接操作原字符串,而是复制一份再进行操作
s = "[Abc]"
print(string.len(s)) --5
print(string.rep(s, 2)) --[Abc][Abc]
print(string.lower(s)) --[abc]
print(string.upper(s)) --[ABC]
--string.sub 截取字符串
--字符索引从前往后是1,2,...;从后往前是-1,-2,...
print(string.sub(s, 2)) --Abc]
print(string.sub(s, -2)) --c]
print(string.sub(s, 2, -2)) --Abc
--string.find(s, pattern, pos) 搜索字符串
--s 源字符串
--pattern 待搜索的模式串
--pos 从pos位置开始搜索
--找到则返回开始和结束的位置,否则返回nil
s = "hello world"
print(string.find(s, "hello")) --1 5
print(string.find(s, "l")) --3 3
print(string.find(s, "lll")) --nil
-- . 任意字符
-- %s 空白符
-- %p 标点字符
-- %c 控制字符
-- %d 数字
-- %x 十六进制数字
-- %z 代表0的字符
-- %a 字母
-- %l 小写字母
-- %u 大写字母
-- %w 字母和数字
--上面字符类的大写形式表示小写所代表的集合的补集。例如,%A 非字母的字符
s = "Deadline is 30/05/1999, firm"
pattern = "%d%d/%d%d/%d%d%d%d"
pattern2 = "%l.%l%s%l%l%s%d."
print(string.sub(s, string.find(s, pattern))) --30/05/1999
print(string.sub(s, string.find(s, pattern2))) --ine is 30
--特殊字符
--( ) . % + - * ? [ ^ $
--对于特殊字符,可以使用 %特殊字符 的方式进行匹配
--%. 匹配.
--%? 匹配?
--%% 匹配%
s = "[[h7o.W%orld]]"
pattern = "%[h7o%.%u%%"
print(string.sub(s, string.find(s, pattern))) --[h7o.W%
-- [] 字符集合
-- [%w_] 匹配字母数字和下划线
-- [01] 匹配二进制数字
-- [%[%]] 匹配一对方括号
-- [0-9] 相当于%d
-- [0-9a-fA-F] 相当于%x
-- [0-7] 相当于[01234567]
-- 在[]中使用^表示其补集
-- [^0-7] 匹配0到7之外的字符
-- [^\n] 匹配非换行字符
-- [^%s] 匹配%S
-- 以上这些都是正则表达式的内容,更详细的可以看:
-- http://blog.csdn.net/lyh916/article/details/49201195
-- 只不过在正则表达式中,lua中使用%代表转义,而java使用\代表转义
--string.gsub(s, pattern, reps) 替换字符串
--s 源字符串
--pattern 待替换的模式串
--pos 替换的内容
--将s中所有符合pattern的字串替换为reps,返回结果串+匹配数
print(string.gsub("hello, world", "o", "a")) --hella, warld 2
--string.gfind(s, pattern)
--返回一个迭代器,迭代器每执行一次,返回下一个匹配串
iter = string.gfind("a=b c=d", "[^%s+]=[^%s+]")
print(iter()) --a=b
print(iter()) --c=d
for s in string.gfind("a=b c=d", "[^%s+]=[^%s+]") do
print(s)
end
--string.format 按一定格式输出字符串
--%s 字符串
--%d int
--%f float
print(string.format("%s add %d = %f", "hello", 2, 3)) --hello add 2 = 3.000000
print(string.format("%05d = %.2f", 20, 3)) --00020 = 3.00
2.math
函数名 | 描述 | 示例 | 结果 |
pi | 圆周率 | math.pi | 3.1415926535898 |
abs | 取绝对值 | math.abs(-2012) | 2012 |
ceil | 向上取整 | math.ceil(9.1) | 10 |
floor | 向下取整 | math.floor(9.9) | 9 |
max | 取参数最大值 | math.max(2,4,6,8) | 8 |
min | 取参数最小值 | math.min(2,4,6,8) | 2 |
pow | 计算x的y次幂 | math.pow(2,16) | 65536 |
sqrt | 开平方 | math.sqrt(65536) | 256 |
mod | 取模 | math.mod(65535,2) | 1 |
modf | 取整数和小数部分 | math.modf(20.12) | 20 0.12 |
randomseed | 设随机数种子 | math.randomseed(os.time()) | |
random | 取随机数 | math.random(5,90) | 5~90 |
rad | 角度转弧度 | math.rad(180) | 3.1415926535898 |
deg | 弧度转角度 | math.deg(math.pi) | 180 |
exp | e的x次方 | math.exp(4) | 54.598150033144 |
log | 计算x的自然对数 | math.log(54.598150033144) | 4 |
log10 | 计算10为底,x的对数 | math.log10(1000) | 3 |
frexp | 将参数拆成x * (2 ^ y)的形式 | math.frexp(160) | 0.625 8 |
ldexp | 计算x * (2 ^ y) | math.ldexp(0.625,8) | 160 |
sin | 正弦 | math.sin(math.rad(30)) | 0.5 |
cos | 余弦 | math.cos(math.rad(60)) | 0.5 |
tan | 正切 | math.tan(math.rad(45)) | 1 |
asin | 反正弦 | math.deg(math.asin(0.5)) | 30 |
acos | 反余弦 | math.deg(math.acos(0.5)) | 60 |
atan | 反正切 | math.deg(math.atan(1)) | 45 |
3.table
--table.concat(table, sep, start, end)
--将table中的元素进行连接,仅适用于数组部分
--sep 连接符
--start 默认值是1
--end 默认值是数组部分的总长
t = {"q", "w", "e"}
print(table.concat(t, " ")) --q w e
t = {a = "q", b = "w", c = "e"}
print(table.concat(t, " ")) --无输出
t = {[1] = "q", [2] = "w", c = "e"}
print(table.concat(t, ",")) --q,w
--table.insert(table, pos, value)
--在table的数组部分指定位置(pos)插入值为value的一个元素. pos参数可选, 默认为数组部分末尾
t = {"q", "w", "e"}
table.insert(t, "r")
print(table.concat(t, " ")) --q w e r
table.insert(t, 2, "t")
print(table.concat(t, " ")) --q t w e r
--table.remove(table, pos)
--table.remove()函数删除并返回table数组部分位于pos位置的元素. 其后的元素会被前移.
--pos参数可选, 默认为table长度, 即从最后一个元素删起.
--table.maxn(table)
--table.maxn()函数返回指定table中所有正数key值中最大的key值. 如果不存在key值为正数的元素, 则返回0.
--此函数不限于table的数组部分.
t = {[1] = "q", [2] = "w", [3] = "e", [26] = "e", }
print(#t) --3 因为26和之前的数字不连续, 所以不算在数组部分内
print(table.maxn(t)) --26
t[91.32] = true
print(table.maxn(t)) --91.32
--table.sort(table, comp)
--table.sort()函数对给定的table进行升序排序.
--comp 接受两个参数(依次为a, b), 并返回一个布尔型的值.如果应该调转位置,则返回false
t = {"q", "w", "e", }
table.sort(t)
print(table.concat(t, ", ")) --e, q, w
table.sort(t, function (a, b) return a>b end)
print(table.concat(t, ", ")) --w, q, e
--多重排序
guild = {}
table.insert(guild, { name = "Cladhaire", class = "Rogue", level = 70, })
table.insert(guild, { name = "Sagart", class = "Priest", level = 70, })
table.insert(guild, { name = "Mallaithe", class = "Warlock", level = 40, })
--对这个table进行排序时, 应用以下的规则: 按等级升序排序, 在等级相同时, 按姓名升序排序.
function SortFunc(a, b)
if a.level == b.level then
return a.name < b.name
else
return a.level < b.level
end
end
table.sort(guild, SortFunc)
for i,v in ipairs(guild) do
print(i,v.name)
end
-- 1 Mallaithe
-- 2 Cladhaire
-- 3 Sagart