您的位置:首页 > 编程语言 > Lua

用lua获取目录,文件名,扩展名

2012-06-11 22:56 204 查看


用lua获取目录,文件名,扩展名

很多时候我们需要从全路径中取得目录,文件名或者扩展名。办法有许多,看lua是怎么做的:

–获取路径

function stripfilename(filename)

return string.match(filename, “(.+)/[^/]*%.%w+$”) — *nix system

–return string.match(filename, “(.+)\\[^\\]*%.%w+$”) — windows

end

–获取文件名

function strippath(filename)

return string.match(filename, “.+/([^/]*%.%w+)$”) — *nix system

–return string.match(filename, “.+\\([^\\]*%.%w+)$”) — *nix system

end

–去除扩展名

function stripextension(filename)

local idx = filename:match(“.+()%.%w+$”)

if(idx) then

return filename:sub(1, idx-1)

else

return filename

end

end

–获取扩展名

function getextension(filename)

return filename:match(“.+%.(%w+)$”)

end

refer to: http://lua-users.org/lists/lua-l/2010-07/msg00088.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: