【问题标题】:Lua How to remove apostrophe(s) from stringLua如何从字符串中删除撇号
【发布时间】:2018-08-24 00:54:12
【问题描述】:

因此,如果 Lua gsub 从字符串中删除撇号,我遇到了一个问题,如果它本身有一个撇号或大量撇号,我似乎无法让它删除其中的任何一个。

local uri_without_args = "'" --one on its own
local uri_without_args = "''''''''lol''''" --in text
local uri_without_args = "''''''''''''" --loads
--etc--etc all occurrences must go

local list = {
"%'", --apostrophe
}
for k,v in ipairs(list) do
    local uri_without_args_remove_duplicates, occurrences = uri_without_args:gsub(""..v.."","")
    if occurrences > 0 then
    occurrences = occurrences + 1
        for i=1, occurrences do
            if uri_without_args_remove_duplicates=="" then
                --do nothing
            else
                uri_without_args = uri_without_args:gsub(""..v.."","")
            end
        end
    end
end


print(uri_without_args)

【问题讨论】:

  • 您的变量uri_without_args_remove_duplicates 包含正确的结果,但您既没有保存也没有打印。

标签: lua


【解决方案1】:

您为uri_without_args 分配新值的唯一时间是uri_without_args_remove_duplicates 不为空。如果您从对uri_without_args 的赋值中删除if 语句,或者如果uri_without_args"''''''''lol''''" 开头,那么它可以正常工作。

正如 Egor 在评论中所说,您也可以简单地使用 uri_without_args_remove_duplicates 作为结果值。

【讨论】:

  • 我按照建议删除了 if 语句,现在可以正常使用了,谢谢 :)
猜你喜欢
  • 1970-01-01
  • 2021-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 2017-03-26
相关资源
最近更新 更多