SoRa
SoRa

Reputation: 23

find a letters in a string from table is only working with the first item

hi i cannot explain it without the lua code the problem is that its just catch the first item of the table if i wrote te = "f... you" it will return true , but if i've wrote , te = "a.." it'll return false -_-

i'm just trying to catch up the bad words from strings , here is the code * sorry for the bad words i just didn't wrote them fully

local te = "f... you"
badwords = {
"f...",
"a..",
"s...",
"b....",
"g..",
"w....",
}

And the function:

function isTextContainsBadwords(str)
  for i,v in ipairs(badwords) do
    if str:find(v) then
      return true
    else
      return false
    end
  end
end
print(tostring(isTextContainsBadwords(te)))

Upvotes: 2

Views: 106

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

This is because when you iterate the table badwords, the first string in it "f..." doesn't match a.. and else block has a return false in it; which terminates the execution of code.

You don't need an else block there. Just take the return false outside of the for loop.

function isTextContainsBadwords(str)
    for i,v in ipairs(badwords) do
        if str:find(v) then
            return true
        end
    end
    return false
end

Upvotes: 1

Related Questions