1

I'm writing a Lua function, and I want to add a if/else statement if url has particular string for example "home", if url look like this:

http://AAA/home
http://AAA/home/01
http://AAA/home/02

Then stop the function, but if url doesn't have "home" string, then do the function:

http://AAA/next
http://AAA/test

Can anyone give me some clue about detecting the particular string in url ?

S.P.
  • 369
  • 6
  • 18

1 Answers1

2

There are two ways you can do this: You can use string.match() or string.find(), I personally use string.find() (most people do).

Your code would look something like this:

text = "http://AAA/home"

if string.find(text, "home?",0,true) then
  # do what you want
else
  # do something else
end

PS : Duplicated question - How to check if matching text is found in a string in Lua?

Community
  • 1
  • 1
hiperbolt
  • 184
  • 2
  • 3
  • 11
  • 2
    Whenever you use string.find, be aware that the string searching for is a regex - so searching for "home?" would match "hom" too. To avoid this, use string.find(text, "home?",0,true) to signify it's a plain string [see the manual](http://www.lua.org/manual/5.1/manual.html#pdf-string.find) – Eike Decker Apr 14 '16 at 08:32