当前位置: 代码网 > it编程>前端脚本>Lua > Lua编程示例(四):Lua标准库之表库、字符串库、系统库

Lua编程示例(四):Lua标准库之表库、字符串库、系统库

2024年05月18日 Lua 我要评论
tb1 = { "alpha","log","gamme"}print(table.concat(tb1," , "))print(table.concat(tb1," ",nil,2))print
tb1 = { "alpha","log","gamme"}

print(table.concat(tb1," , "))
print(table.concat(tb1,"\n",nil,2))
print()

tb1[88.99] = 'aaa'
--返回索引值最大的值,并且计算小数
print(table.maxn(tb1))
print()

--默认删除索引最大的元素并返回
print(table.remove(tb1))
print()

table.insert(tb1,'3423')
table.sort(tb1)
print(table.concat(tb1," , "))
function sortfunc(a,b)
 return a>b
end
table.sort(tb1,sortfunc)
print(table.concat(tb1," , "))
print()

print(string.len(""))
print(string.len("abcd"))
print(string.sub("welcome",1,3))
print(string.sub("welcome",1,-1))
print(string.sub("welcome",1,-5))
print()

print(os.date())
print()

do
 local x = os.clock()
 local s = 0
 for i=1,100000000 do s= s+i end
 print(string.format("the passed time is %.2f\n",os.clock()-x))
end

s = "hello world"
i, j = string.find(s, "hello")
print(i, j)    --> 1  5
print(string.sub(s, i, j))  --> hello
print(string.find(s, "world"))  --> 7  11
i, j = string.find(s, "l")
print(i, j)    --> 3  3
print(string.find(s, "lll"))  --> nil

s = string.gsub("lua is super", "is","makes")
print(s)
s = string.gsub(s,"a","x")
print(s)
s = string.gsub(s,"x","a",1)
print(s)

运行结果:

alpha , log , gamme
alpha
log

88.99

gamme

3423 , alpha , log
log , alpha , 3423

0
4
wel
welcome
wel

11/24/11 17:55:44

the passed time is 2.12

1 5
hello
7 11
3 3
nil
lua makes super
lux mxkes super
lua mxkes super

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com