【1】文件名称类型判断和解析
local fileName = "shanxi201904npsdr1_200000.zip" print("len : " .. string.len(fileName)) local start, length = string.find(fileName, ".zip") print("start : " .. start .. " length : " .. length) if length == string.len(fileName) then local index = string.find(fileName, "_") print("index : " .. index) , index - ) , start - ) print("before_index : " .. before_index) print("after_index : " .. after_index) else print("file is not zip type!") end --[[ len : 29 start : 26 length : 29 index : 19 before_index : shanxi201904npsdr1 after_index : 200000 --]]
程序解析:
第4行:利用find函数查找“.zip”后缀,判断文件类型是否为zip压缩包。此函数返回两个值,start为.zip的起始索引,length为结束索引。
第6行:判断length结束索引与文件名字符串长度相等,相等即为zip文件;不等即不是zip文件。
第7行:利用find函数查找“_”字符,按文件名命名规则,字符“_”之前的内容为文件名,字符“_”之后的内容为文件总行数。返回查找到的索引。网址:yii666.com
第10行:利用sub函数,获取子串,字符“_”之前的文件名内容。
第11行:利用sub函数,获取子串,字符“_”之后的文件总行数内容值:200000。文章来源地址:https://www.yii666.com/article/758359.html
注意一点:lua的下标从1开始,所以对应理解第10、11行的索引起始写法。
Good Good Study, Day Day Up.
顺序 选择 循环 总结
文章地址https://www.yii666.com/article/758359.html