如何检查Lua中字符串中是否找到匹配的文本?

前端之家收集整理的这篇文章主要介绍了如何检查Lua中字符串中是否找到匹配的文本?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如果在文本字符串中至少找到一个特定的匹配文本,我需要使条件为真,例如:
str = "This is some text containing the word tiger."
if string.match(str,"tiger") then
    print ("The word tiger was found.")
else
    print ("The word tiger was not found.")

如何检查文本是否在字符串中的某处找到?

解决方法

你可以使用string.match或string.find。我个人自己使用 string.find()。此外,您需要指定if-else语句的结尾。所以,实际的代码将如下:
str = "This is some text containing the word tiger."
if string.match(str,"tiger") then
  print ("The word tiger was found.")
else
  print ("The word tiger was not found.")
end

要么

str = "This is some text containing the word tiger."
if string.find(str,"tiger") then
  print ("The word tiger was found.")
else
  print ("The word tiger was not found.")
end

猜你在找的Lua相关文章