在 Lua 中的字典中查找字符串

所以我有一个字典:

local toFind = "Layla"

local songs = {
    {name = "Eric Clapton - Layla", id = "123"},
    {name = "Eric Clapton - I Shot The Sheriff", id = "321"}
}

是否有可能使用 string.find(songs, toFind) 或类似的方式找到包含 "Layla" 字符串的表格?

原文链接 https://stackoverflow.com/questions/69470668

点赞
stackoverflow用户11740758
stackoverflow用户11740758

所有字符串都附带了字符串函数作为方法。

因此,你可以直接这样做...

for k,v in pairs(songs) do
 if v.name:find(toFind) then
  return songs[k], v.name
 end
end

...这将返回表格和完整文本...

table: 0x565cbec0   Eric Clapton - Layla
2021-10-06 20:10:27