如果在文本字符串中至少找到一个特定的匹配文本,我需要使条件为真,例如:
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