Lua如何删除".html"字符串末尾的文字 [英] Lua how to remove ".html" text from end of string
本文介绍了Lua如何删除".html"字符串末尾的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我在Lua中有一个字符串,我想删除字符串末尾的所有出现的".html"文本
So I have a string in Lua and I want to remove all occurances ".html" text of the end of the string
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
推荐答案
首先,您可以定义如下函数:
First, you could define a function like this:
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
方法返回的第二个值指示匹配模式的次数.例如,它返回带有 path1
的 1
和带有 path2
的 2
.(以防万一信息对您有用)
There is a second value returned from the gsub
method that indicates how many times the pattern was matched. It returns, for example, 1
with path1
and 2
with path2
. (Just in case that info is useful to you):
local path2, occurrences = removeHtmlExtensions(lol2)
print(occurrences) -- 2
这篇关于Lua如何删除".html"字符串末尾的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文