String:find()

The find method on a string allows you to match find the start and end position (index) of the first match of a pattern you specify.

Copy paste this code into the translator:

local String = "Take control of your data" local Start, End = String:find("control")

The Colon Operator shorthand passes the String object (the “self“) as the 1st argument.

In the Translator’s Annotation Windows , you should see that the starting character for “control“ is at index 6 and the end at index 12.

Optionally, you can specify additional arguments. A 3rd argument can be used to set the search start position. This index can be negative to start the search from the end of the string.

By default, find uses Lua's built in Pattern Matching. For example:

local String = "Take control of your data" local Start, End = String:find("con....", 5) -- starts search at position 5 and returns positions 6,12

We can turn off pattern matching by supplying a 4th argument set to true. For example, when magic or special characters are used:

local String = "The %d is important." local Start, End = String:find('%d', 1, true) -- returns positions 5,6

 

 

Related pages