lua中table 库函数 insert、remove、concat、sort的简单使用【转】

1、insert

1 do
2     t1 = {"欲", "泪", "成", "雪"}
3     table.insert(t1,"seeyou")-- 往t1末尾插入元素 "seeyou"
4     table.insert(t1, 3, "bug")-- 往t1索引为3的位置插入元素"bug"
5     for i,v in ipairs(t1) do print(i,v) end
6 end

执行结果:

2、remove

1 do
2     t2 = {"see","you","欲", "泪", "成", "雪","bug"}
3     table.remove(t2)-- 移除t2中末尾元素 "bug"
4     table.remove(t2, 3)-- 移除t2中索引位置为3的元素"欲"
5     for i,v in ipairs(t2) do print(i,v) end
6 end

执行结果:

3、concat

1 do
2     t3 = {"see","you","bug","欲", "泪", "成", "雪"}
3     print(table.concat(t3)) -- 对t3进行字符串拼接
4     print(table.concat(t3,"|")) -- 添加分割符
5     print(table.concat(t3,"-"))
6 end

执行结果:

4、sort

1 do
2     t4 = {"c","b","a", "d","e"}
3     table.sort(t4)-- 默认对t4进行升序排序
4     for i,v in ipairs(t4) do print(i,v) end
5     print("----------^_^------------")
6     table.sort(t4,function(a,b)return (a> b) end) -- 降序
7     for i,v in ipairs(t4) do print(i,v) end
8 end

执行结果:

 
文章转自:https://www.cnblogs.com/SeeYouBug/p/9594527.html

原文地址:https://www.cnblogs.com/KillBugMe/p/13151818.html