string.upper(str):字符串全部大写。
string.;pwer(str):字符串全部小写。
string.gsub(str,"i","123",5):替换字符串的内容,str字符串,“i”要替换的字符,“123”替换成的字符,“5”最大替换次数。
string.find(str,"name",10):返回所查找到的位置的索引。
string.format("%d+%d=%d",num1,num2,(num1+num2));
table连接。
table={"111","222","333"}
print(table.concat(mytable))
print(table.concat(mytable,","))
print(table.concat(mytable,",",2,3))
table的插入。
table.insert(mytable,"444")。再最后一位插入数据
table.insert(mytable,2,"555")。在指定索引位置插入
table的删除。
table.remove(mytable,2)。移除指定索引的数据
require “模块名”。引入模块
requore "module"
print(module.var)。
module.func()。
mytable={“111”,“222”,“333”,“444”,“555”}
mymetatable={ } 。元表
mytable=setmetatable(mytable,mymetatable)
1。__index元方法
mymetatable={__index=fuction(tab,key) }。访问一个不存在的key的时候,会调用该函数
2。__newindex元方法。给表添加一个新键值对的时候,将key值修改为value
mymetatable={
__newindex=funtion(tab,key,value)
rawset(tab,key,value)
end
}。
3。__add元方法。表加法运算
_add=fuction(tab,newtab)
for k,v in pairs(tab) do
if(k>i) then
i=k
end
end
for k,v in pairs(newtab) do
i=i+1
table.insert(tab,i,v)
end
end
4。__call元方法。把表当做函数使用时,调用该方法。
table(index)
5.__tostring元方法、把表当做字符串使用时,调用该方法
协程coroutine。
//定义协同函数
co=coroutine.create(
fuction (a,b)//匿名函数,不需要方法名
end
)
co=coroutine.wrap()。//不需要使用resume函数启动。
co(20,30)
//启动协程,继续协程
coroutine.resume(co,20,30)。
//挂起协程
coroutine.yield()。
//获取当前协程的执行状态
coroutine.resume()。
//获取当前正在运行的协程
coroutine.running()。
lua文件I/O
简单模式。
file=io.open("filename"."r")。r只读方式打开,w打开文件,写入新内容会清除文件原有内容、a打开文件写入时保留原有内容。
io.inptu(file)
io.read()。读取一行文件内容。*n读取一个数字返回,*a读取全部内容并输出,number取得输入数量的内容。
io.close()。
io.write()。写入内容
完全模式。
file=io.open("filename"."a")
file:write("1111")
file.close()
lua面向对象-table function。