如何检查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
http://stackoverflow.com/questions/10158450/how-to-check-if-matching-text-is-found-in-a-string-in-lua

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:如何检查Lua中字符串中是否找到匹配的文本?