0

My question is how can I replace the character from a given string? If someone says something in the chat, and that word is a bad word then replace it with "****". I don't know how to approach the print section.. Because I've got the index of the badword but I don't know how to replace it from the original message.

local badwords = {
    "badword",
    "badword2",
}

function(msg)
   local message = msg;
   local lowered_msg = message:lower(); 

   for index, val in pairs(badwords) do 
       if lowered_msg:match(val) then
           local indexStart, indexEnd = string.find(lowered_msg, val)
           print(message:gsub(indexStart, "*****", indexEnd))
       end
   end
end 

2 Answers 2

1

The gsub function takes three arguments:

  • the string to be modified,
  • the pattern to be replaced,
  • and the replacement string,

so you need to do something like this:

message = string.gsub(message, message:sub(indexStart, indexEnd), "*****")
print(message)
Sign up to request clarification or add additional context in comments.

Comments

0

This Way use a table with badwords and replacements for gsub.

local badwords = {fair='f**r', foul='f**l'}
print(('fair is foul and foul is fair'):gsub('%w+', badwords))

...if the pattern matches words.
gsub() loops over the whole string except the 4th argument is used enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.