使用 string.find 在列表中查找字符串(Lua)

我正在为一个名为"Stormworks"的游戏编写 Lua 脚本,但我不知道如何在列表中搜索某个单词

目前我正在使用以下方法,但它说它不能在列表中使用

if string.find(message,Word_list)
then
server.announce("[Server]", "嘿!"..sender_name.."注意你的语言")
end
点赞
用户9922866
用户9922866

string.find 不接受 table 作为参数。除非您正在查找字符串中的特定模式,否则没有必要使用 string.find 检查字符串的相等性;使用 == 运算符代替。

如果您有一个包含 n 个字符串的表,并且您正在搜索特定的字符串(同样,只是简单的相等性),请遍历该表并检查每个元素。

-- Requires: tbl is a table containing strings; str is a string.
-- Effects : returns true if tbl contains str, false otherwise.
local function find_string_in(tbl, str)
    for _, element in ipairs(tbl) do
        if (element == str) then
            return true
        end
    end
    return false
end

local t = {"hello", "there", "friend"}
print(find_string_in(t, "friend"))
print(find_string_in(t, "goodbye"))

这会产生以下输出:

true
false
2020-10-03 16:46:08