用字符在表格中查找数值

我有一个包含数值的表格,我想返回所有在 search_str 中包含这些字符的 val value。

local items_array = {
'apple','app','hello', 'app21'
}

local function test(str)
for k , v in pairs(items_array) do
    print(string.match( str, v ), v)
    // print(string.match( str, ".*"..v..".*" )) // 这个不起作用
end
end
test('a') // 但这意味着 apple , app , app21 被返回了
// 返回

nil apple
nil app
nil hello
nil app21
点赞
用户2858170
用户2858170

你可以使用 string.find 或者 string.match 来检查你的表元素字符串是否包含了搜索的字符串。

请参考 Lua 手册获取更基础的信息。

2019-12-17 17:32:45