2

I'm tryng to check in lua if AGM-65D is found in "LAU-117,AGM-65D"

but string.find("LAU-117,AGM-65D", "AGM-65D") and string.match("LAU-117,AGM-65D", "AGM-65D")

both return nil. Does someone could suggest me why? maybe the comma or the "-" symbol must be used in a different way?

thanks

1
  • 2
    string.find("LAU-117,AGM-65D", "AGM-65D", 1, true) Commented Dec 26, 2018 at 13:16

1 Answer 1

2

Both string.find and string.match will search for a pattern, and not for a plain string. And your search fails because symbol - is a special character in pattern matching.

To make it work, "escape" special characters with % sign, make it "AGM%-65D"

edit: A comment from Egor reminds us there's optional argument to string.find to make it search for a plain text. In simple search cases it should be preferred.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! sadly I got the "AGM-65D" string from another chunk, so I need to find a way to replace - with %-. string.gsub obviously didn't do that.
found!... I had to escape the escape character, so: string.gsub("AGM-65D", "-", "%%-" ) produces "AGM%-65D" as needed
@user3204845 See the comment from Egor, there's often overlooked option that makes string.find to search for plain texts :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.