gpt4 book ai didi

string - 如何在Lua中实现string.rfind

转载 作者:行者123 更新时间:2023-12-04 03:58:34 26 4
gpt4 key购买 nike

在Lua中,只有string.find,但有时需要string.rfind。例如,要解析目录和文件路径,例如:

fullpath = "c:/abc/def/test.lua"
pos = string.rfind(fullpath,'/')
dir = string.sub(fullpath,pos)

怎么写这样的 string.rfind

最佳答案

您可以使用string.match:

fullpath = "c:/abc/def/test.lua"
dir = string.match(fullpath, ".*/")
file = string.match(fullpath, ".*/(.*)")

在此模式中, .*是贪婪的,因此它将在匹配 /之前尽可能多地匹配

UPDATE :

正如@Egor Skriptunoff指出的那样,这更好:
dir, file = fullpath:match'(.*/)(.*)'

关于string - 如何在Lua中实现string.rfind,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17386792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com