如何在Lua中获取字符串中字符的最后一个索引?
"/some/path/to/some/file.txt"
如何获取上述字符串中最后一个/
的索引?
如何在Lua中获取字符串中字符的最后一个索引?
"/some/path/to/some/file.txt"
如何获取上述字符串中最后一个/
的索引?
index = string.find(your_string, "/[^/]*$")
(基本上,找到模式“一个正斜杠,然后是零个或多个不是正斜杠的东西,然后是字符串结尾”的位置。)
index = your_string:match'^.*()/'
function fileExtension(path)
local lastdotpos = (path:reverse()):find("%.")
return (path:sub(1 - lastdotpos))
end
local function basename(path)
return path:sub(path:find("/[^/]*$") + 1)
end
local s = "/aa/bb/cc/dd/ee.txt"
local sep = "/"
local lastIndex = nil
local p = string.find(s, sep, 1)
lastIndex = p
while p do
p = string.find(s, sep, p + 1)
if p then
lastIndex = p
end
end
print(lastIndex)