--- 得到 lua 模块路径的上 n 级,返回的的字符串结尾有个 "." 字符 -- @usage getUpFloder("math.base.core", 2) --将返回 "math." -- @string 想要处理的模块路径字符串 -- @int 向上 n 级,可选参数,默认为 1 local function getUpFloder(path, n) -- 错误检查 path = path or "" n = n or 1 assert(type(path) == "string", "传入的参数不是字符串类型!") -- 找到文件夹的每一级 local path_part = {} for w in string.gmatch(path, "%.?[^%.]+") do path_part[#path_part + 1] = w end -- 生成需要的部分 local new_path = "" for i = 1, #path_part - n do new_path = new_path .. path_part[i] end -- 返回 return new_path .. "." end
欢迎指错,当然,如果你有更好的方法也请@我告诉我!