Lua .find()
The .find() function is used to search for a substring within a string and returns the start and end indices of the first occurrence of the substring. If the substring is not found, the function returns nil.
Syntax
These are 2 different syntaxes for the .find() function:
string.find(fullString, searchString, init, pattern)
This requires assigning the function to a variable, to return the function.
startindex, endindex = string.find(fullstring, searchstring, init, pattern)
This uses multiple assignments to save the results to individual variables.
fullString: This is the string being searched through.searchString: This is the sub-string to search for.init(optional): This is the initial index where the search starts. Default index is 1.pattern(optional): This is a Boolean flag indicating whether to search using pattern matching. When set to false, pattern matching can be used, whereas when set to true, pattern matching is ignored, and exact matches are searched for. Default is false.
Note: The special pattern-matching characters allow for lua to use specified characters in
searchStringto make a pattern-matching search. This allows for complex search patterns, which can enhance the flexibility of your searches. Pattern matching is similar to the concept Regular Expressions, although Lua’s pattern matching is not the same as a regular expression, as it is more limited, and uses a different syntax.
Example
Using .find() without multiple assignments
The below example assigns the function to a unique variable, in order for the results to be returned/printed to the console.
sentence = 'hello world'search1 = string.find(sentence, "hello")search2 = string.find(sentence, "world")search3 = string.find(sentence, "universe")print(search1)print(search2)print(search3)
The output will be:
17nil
hellois found at starting index 1 in thesentencestring.worldis found at starting index 7 in thesentencestring.universeis not found insentencestring, therefore the output is nil.
Using .find() with multiple assignments
The below example uses multiple assignments to save the results to individual variables, enabling the results to be returned or printed to the console.
sentence = "hello world"a, b = string.find(sentence, "hello")print(a)print(b)print(a, b)y, z = string.find(sentence, "world")print(y)print(z)print(y, z)
The output will be:
151 57117 11
hellostarts at index 1, and ends at index 5 in thesentencestring. Due to multiple assignments,asaves the value1, andbsaves the second value5.worldstarts at index 7, and ends at index 11 in thesentencestring. Due to multiple assignments,ysaves the first value7, andzsaves the second value11.
Using .find() with init (Initial position) parameter
The below example uses the initial position parameter to indicate where the search should start.
sentence1 = 'hello world'search = string.find(sentence1, "l", 5)print(search)sentence2 = 'world world'a, b = string.find(sentence2, "orl", 5)print(a)print(b)print(a, b)
The output will be:
108108 10
lhas multiple instances within thesentencestring. Due to the init parameter, the search starts at index 5, therefore the first instance oflfrom index 5 will be at starting index10.orlhas multiple instances within thesentencestring. Due to the init parameter, the search starts at index 5, therefore the first instance oforlfrom index 5 will be at starting index8, and ends at index10.
Using .find() with pattern (pattern matching) parameter
The below example uses the pattern-matching parameter to determine if pattern-matching characters can be used as search terms.
-- With pattern matchingsentence = "hello world"a, b = string.find(sentence, "l+", 1, false)print(a)print(b)-- Without pattern matchingy, x = string.find("hello world", "l+", 1, true)print(y)print(z)
The output will be:
34nilnil
- In the first example,
l+uses+to match one or more repetitions of the previous characterl. Due to thefalseboolean value, pattern matching is being used, which results in the outputs ofaandbresulting in 3 and 4 respectively. - In the second example,
l+is being treated as a literal string, which doesn’t exist in the stringhello world. Due to thetrueboolean value, pattern matching is disabled, which results in the outputs ofnilin bothyandz, as the stringl+does not exist in the stringhello world.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
- Beginner Friendly.4 hours