gpt4 book ai didi

Lua如何从字符串末尾删除 ".html"文本

转载 作者:行者123 更新时间:2023-12-04 01:53:28 27 4
gpt4 key购买 nike

所以我在 Lua 中有一个字符串,我想删除字符串末尾的所有出现的“.html”文本

local lol = ".com/url/path/stuff.html"

print(lol)

我想要的输出:
.com/url/path/stuff


local lol2 = ".com/url/path/stuff.html.html"

print(lol2)

我想要的输出:
.com/url/path/stuff

最佳答案

首先,你可以定义一个这样的函数:

function removeHtmlExtensions(s)
return s:gsub("%.html", "")
end

然后:
local lol = ".com/url/path/stuff.html"
local lol2 = ".com/url/path/stuff.html.html"

local path1 = removeHtmlExtensions(lol)
local path2 = removeHtmlExtensions(lol2)

print(path1) -- .com/url/path/stuff
print(path2) -- .com/url/path/stuff
gsub 返回了第二个值指示模式匹配多少次的方法。例如,它返回 1path12path2 . (以防万一该信息对您有用):
local path2, occurrences = removeHtmlExtensions(lol2)

print(occurrences) -- 2

关于Lua如何从字符串末尾删除 ".html"文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51868289/

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