Find text within a string

Verified
Added by iNTERFACEWARE

Find the position of the first instance of a string (pattern) within a string, returns the start and end indices

Source Code
   -- search from start of string finds first instance of "Lua"
   local s= 'Hello Lua user are you enjoying Lua?'
   local ix1, ix2=s:find('Lua') --> 7,9
   
   -- search from char 10 finds second instance of "Lua"
   local s= 'Hello Lua user are you enjoying Lua?'
   local ix1, ix2=s:find('Lua', 10) --> 33,35
   
   -- find is case sensitive - no match returns nil
   local s= 'Hello Lua user are you enjoying Lua?'
   local ix1, ix2=s:find('LUA') --> nil
Description
Find the position of the first instance of a string (pattern) within a string, returns the start and end indices
Usage Details

Use string.find() to find the first instance of a string (pattern) within a string.

How to use the snippet:

  • Paste the code into your script
  • Inspect the annotations to see how it works