[Unity热更新]lua常用库

本文详细介绍了Lua语言中字符串的处理方法,包括查找、替换、格式化等实用技巧,并深入探讨了Lua提供的数学库函数及其应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From:http://blog.youkuaiyun.com/lyh916/article/details/53573385


1.string

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. --string库中所有的function都不会直接操作原字符串,而是复制一份再进行操作  
  2.   
  3. s = "[Abc]"  
  4. print(string.len(s))          --5  
  5. print(string.rep(s, 2))       --[Abc][Abc]  
  6. print(string.lower(s))        --[abc]  
  7. print(string.upper(s))        --[ABC]  
  8.   
  9.   
  10. --string.sub 截取字符串   
  11. --字符索引从前往后是1,2,...;从后往前是-1,-2,...  
  12. print(string.sub(s, 2))       --Abc]  
  13. print(string.sub(s, -2))      --c]  
  14. print(string.sub(s, 2, -2))   --Abc  
  15.   
  16.   
  17. --string.find(s, pattern, pos) 搜索字符串  
  18. --s 源字符串   
  19. --pattern 待搜索的模式串  
  20. --pos 从pos位置开始搜索  
  21. --找到则返回开始和结束的位置,否则返回nil  
  22. s = "hello world"  
  23. print(string.find(s, "hello"))    --1 5  
  24. print(string.find(s, "l"))        --3 3  
  25. print(string.find(s, "lll"))      --nil  
  26.   
  27. -- . 任意字符  
  28. -- %s 空白符  
  29. -- %p 标点字符  
  30. -- %c 控制字符  
  31. -- %d 数字  
  32. -- %x 十六进制数字  
  33. -- %z 代表0的字符  
  34. -- %a 字母  
  35. -- %l 小写字母  
  36. -- %u 大写字母  
  37. -- %w 字母和数字  
  38. --上面字符类的大写形式表示小写所代表的集合的补集。例如,%A 非字母的字符  
  39. s = "Deadline is 30/05/1999, firm"  
  40. pattern = "%d%d/%d%d/%d%d%d%d"  
  41. pattern2 = "%l.%l%s%l%l%s%d."  
  42. print(string.sub(s, string.find(s, pattern)))    --30/05/1999  
  43. print(string.sub(s, string.find(s, pattern2)))   --ine is 30  
  44.   
  45. --特殊字符   
  46. --( ) . % + - * ? [ ^ $  
  47. --对于特殊字符,可以使用 %特殊字符 的方式进行匹配  
  48. --%. 匹配.  
  49. --%? 匹配?  
  50. --%% 匹配%  
  51. s = "[[h7o.W%orld]]"  
  52. pattern = "%[h7o%.%u%%"  
  53. print(string.sub(s, string.find(s, pattern)))    --[h7o.W%  
  54.   
  55. -- [] 字符集合  
  56. -- [%w_]        匹配字母数字和下划线  
  57. -- [01]         匹配二进制数字  
  58. -- [%[%]]       匹配一对方括号  
  59. -- [0-9]        相当于%d  
  60. -- [0-9a-fA-F]  相当于%x  
  61. -- [0-7]        相当于[01234567]  
  62.   
  63. -- 在[]中使用^表示其补集  
  64. -- [^0-7]       匹配0到7之外的字符  
  65. -- [^\n]        匹配非换行字符  
  66. -- [^%s]        匹配%S  
  67.   
  68.   
  69. -- 以上这些都是正则表达式的内容,更详细的可以看:  
  70. -- http://blog.youkuaiyun.com/lyh916/article/details/49201195  
  71. -- 只不过在正则表达式中,lua中使用%代表转义,而java使用\代表转义  
  72.   
  73.   
  74. --string.gsub(s, pattern, reps) 替换字符串  
  75. --s 源字符串   
  76. --pattern 待替换的模式串  
  77. --pos 替换的内容  
  78. --将s中所有符合pattern的字串替换为reps,返回结果串+匹配数  
  79. print(string.gsub("hello, world""o""a")) --hella, warld 2  
  80.   
  81.   
  82. --string.gfind(s, pattern)  
  83. --返回一个迭代器,迭代器每执行一次,返回下一个匹配串  
  84. iter = string.gfind("a=b c=d""[^%s+]=[^%s+]")  
  85. print(iter()) --a=b  
  86. print(iter()) --c=d  
  87.   
  88. for s in string.gfind("a=b c=d""[^%s+]=[^%s+]"do  
  89.     print(s)  
  90. end  
  91.   
  92.   
  93. --string.format 按一定格式输出字符串  
  94. --%s 字符串  
  95. --%d int  
  96. --%f float  
  97. print(string.format("%s add %d = %f""hello", 2, 3)) --hello add 2 = 3.000000  
  98. 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

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. --table.concat(table, sep,  start, end)  
  2. --将table中的元素进行连接,仅适用于数组部分  
  3. --sep 连接符  
  4. --start 默认值是1  
  5. --end 默认值是数组部分的总长  
  6. t = {"q""w""e"}  
  7. print(table.concat(t, " "))       --q w e  
  8.   
  9. t = {a = "q", b = "w", c = "e"}  
  10. print(table.concat(t, " "))       --无输出  
  11.   
  12. t = {[1] = "q", [2] = "w", c = "e"}  
  13. print(table.concat(t, ","))       --q,w  
  14.   
  15.   
  16. --table.insert(table, pos, value)  
  17. --在table的数组部分指定位置(pos)插入值为value的一个元素. pos参数可选, 默认为数组部分末尾  
  18. t = {"q""w""e"}  
  19. table.insert(t, "r")  
  20. print(table.concat(t, " "))       --q w e r  
  21. table.insert(t, 2, "t")  
  22. print(table.concat(t, " "))       --q t w e r  
  23.   
  24.   
  25. --table.remove(table, pos)  
  26. --table.remove()函数删除并返回table数组部分位于pos位置的元素. 其后的元素会被前移.   
  27. --pos参数可选, 默认为table长度, 即从最后一个元素删起.  
  28.   
  29.   
  30. --table.maxn(table)  
  31. --table.maxn()函数返回指定table中所有正数key值中最大的key值. 如果不存在key值为正数的元素, 则返回0.   
  32. --此函数不限于table的数组部分.  
  33. t = {[1] = "q", [2] = "w", [3] = "e", [26] = "e", }  
  34. print(#t)              --3 因为26和之前的数字不连续, 所以不算在数组部分内  
  35. print(table.maxn(t))   --26  
  36. t[91.32] = true  
  37. print(table.maxn(t))   --91.32  
  38.   
  39.   
  40. --table.sort(table, comp)  
  41. --table.sort()函数对给定的table进行升序排序.  
  42. --comp 接受两个参数(依次为a, b), 并返回一个布尔型的值.如果应该调转位置,则返回false  
  43. t = {"q""w""e", }  
  44. table.sort(t)  
  45. print(table.concat(t, ", "))    --e, q, w  
  46. table.sort(t, function (a, b) return a>b end)  
  47. print(table.concat(t, ", "))    --w, q, e  
  48.   
  49.   
  50. --多重排序  
  51. guild = {}  
  52. table.insert(guild, { name = "Cladhaire"class = "Rogue",   level = 70, })  
  53. table.insert(guild, { name = "Sagart",    class = "Priest",  level = 70, })  
  54. table.insert(guild, { name = "Mallaithe"class = "Warlock", level = 40, })  
  55. --对这个table进行排序时, 应用以下的规则: 按等级升序排序, 在等级相同时, 按姓名升序排序.  
  56. function SortFunc(a, b)  
  57.     if a.level == b.level then  
  58.         return a.name < b.name  
  59.     else  
  60.         return a.level < b.level  
  61.     end  
  62. end  
  63. table.sort(guild, SortFunc)  
  64. for i,v in ipairs(guild) do  
  65.     print(i,v.name)  
  66. end  
  67. -- 1    Mallaithe  
  68. -- 2    Cladhaire  
  69. -- 3    Sagart  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值