How to replace certain letters of a sentence?

Asked

Viewed 1,593 times

3

I want to replace these letters:

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

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

local words = {
   ["v"] = "d",
   ["s"] = "k"
}

Supposing I have the phrase Insira o texto, the desired result would be infira o fexfo, replacing the s and t for f.

  • You can edit the question and set an example of what you want to do?

  • Let’s say this is the text : "Insert the text", it would return that "infira the fexfo"

  • What would that be in?

1 answer

4


You can use the function string.gsub to make the replacements, the default %a represents all letters:

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

To use it, do:

-- 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

  • Thank you very much, dear

  • @Joaogabriel Dispo. If possible mark the answer as accepted by clicking on the mark below the score. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.