String Lua:删除字符串中的特定字符

String Lua:删除字符串中的特定字符,string,replace,lua,String,Replace,Lua,我有一个字符串,它包含所有应该是 在给定字符串中删除。通过嵌套循环,我可以迭代 两条线。但是有没有一条更短的路 local ignore = "'`'" function ignoreLetters( c ) local new = "" for cOrig in string.gmatch(c,".") do local addChar = 1 for cIgnore in string.gmatch(ignore,".") do if cOrig =

我有一个字符串,它包含所有应该是 在给定字符串中删除。通过嵌套循环,我可以迭代 两条线。但是有没有一条更短的路

local ignore = "'`'"  

function ignoreLetters( c )
  local new = ""
  for cOrig in string.gmatch(c,".") do
    local addChar = 1
    for cIgnore in string.gmatch(ignore,".") do
      if cOrig == cIgnore then
        addChar = 0
        break  -- no other char possible
      end
    end
    if addChar>0 then new = new..cOrig end
  end
  return new
end

print(ignoreLetters("'s-Hertogenbosch"))
print(ignoreLetters("'s-Hertogen`bosch"))
如果字符串
ignore
使代码更短,则该字符串也可以是一个表。

您可以使用它将字符串中给定字符串的任何出现替换为另一个字符串。要删除不需要的字符,只需将其替换为空字符串即可

local ignore = "'`'"  

function ignoreLetters( c )
  return (c:gsub("["..ignore.."]+", ""))
end

print(ignoreLetters("'s-Hertogenbosch"))
print(ignoreLetters("'s-Hertogen`bosch"))
只是要知道,如果你想忽略魔法角色,你必须在游戏中逃离它们。 但我想这会给你一个起点,让你有很多自己的工作需要完善