How can i check if a string contains a word from a table

How can i check if a string contains a selection of words in a table.

Example

local randomTable = {“hi”}

----check if strings contains hi

1 Like

You could use table.find to look for the string.

Example:

table.find(randomTable, "hi")

local Table = {"e", "hello", "E"}

print(table.find(Table, "hello"))-- Output: 2
6 Likes

If you want to find substrings rather than comparing the whole string:

local randomTable = {"hi"}

local function hasKeyword(s)
	for _, keyword in ipairs(randomTable) do
		if (s:find(keyword, 1, true)) then
			return true
		end
	end
	return false
end

print(hasKeyword("hello")) --> false
print(hasKeyword("hi there")) --> true
8 Likes