How to replace certain letters in a sentence?

3

I want to replace these letters:

local words = {"v", "s ", "#@#", "s", "รง", "b", "mp", "t"}

When they were in a sentence they would be replaced, or it could be a table like that too:

local words = {
   ["v"] = "d",
   ["s"] = "k"
}
Assuming I have the phrase Insira o texto , the desired result would be infira o fexfo , replacing s and t with f .

    
asked by anonymous 29.05.2015 / 13:16

1 answer

4

You can use string.gsub function to do the substitutions, the %a pattern represents all letters:

function substituirLetras(texto, traducoes)
    return string.gsub(texto, "%a", traducoes)
end

To use it, do the following:

-- chave: o valor a ser substituído
-- valor: o substituto
letras = { 
   ["s"] = "f",
   ["t"] = "f",
}

texto = "Insira o texto"

resultado = substituirLetras(texto, letras)
print (resultado) -- Infira o fexfo

See demonstração

    
29.05.2015 / 14:24