string.find()
Part of the Fundamentals section of Coddy's Lua journey — lesson 48 of 90.
The string.find() function is a powerful tool for searching within strings. It allows you to look for a smaller string (called a pattern) inside a larger string and tells you exactly where it was found.
Here's the basic syntax:
string.find(text, pattern)When string.find() successfully locates the pattern, it returns two numbers: the starting position and the ending position of the match. If the pattern is not found, it returns nil.
Here's a simple example:
local sentence = "The treasure is hidden here"
local start_pos, end_pos = string.find(sentence, "treasure")
print(start_pos, end_pos) -- outputs: 5 12In this example, the word "treasure" starts at position 5 and ends at position 12 in the sentence.
Challenge
EasyCreate a secret message detector that searches for a hidden code word within a larger text message. Declare a variable called message and assign it the value "The ancient treasure lies beneath the old oak tree". Use the string.find() function to search for the word "treasure" within the message and store the starting and ending positions in variables called startPos and endPos. Print both position values on separate lines.
Cheat sheet
The string.find() function searches for a pattern within a string and returns the starting and ending positions of the match, or nil if not found.
Basic syntax:
string.find(text, pattern)Example:
local sentence = "The treasure is hidden here"
local start_pos, end_pos = string.find(sentence, "treasure")
print(start_pos, end_pos) -- outputs: 5 12Try it yourself
-- Declare the message variable
local message = "The ancient treasure lies beneath the old oak tree"
-- TODO: Write your code here to find the word "treasure" and store positions
-- Print the positions
print(startPos)
print(endPos)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators 2 Relational & Logic
Equality OperatorsRelational OperatorsThe 'and' OperatorThe 'or' OperatorThe 'not' OperatorShort-Circuit EvaluationTruthy and Falsy ValuesRecap - Simple Logic7Basic Conditional Logic
The if-then StatementThe if-then-else StatementThe elseif StatementRecap - Treasure Chest2Variables and Data Types
What is a Variable?NumbersStringsBooleansThe Value 'nil'The type() FunctionNaming ConventionsRecap - Character Profile5Basic Output
Printing LiteralsPrinting VariablesPrinting Multiple ValuesCombining Strings & VariablesThe tostring() FunctionInputCastRecap - Status ReportRecap - Till 1208String Manipulation Basics
string.len()string.upper & string.lowerstring.sub()string.rep()string.find()Recap - Format Username3Operators 1 Arithmetic & Conc
Arithmetic OperatorsModulo OperatorExponentiation OperatorString ConcatenationOperator PrecedenceRecap - Simple Calculations6Project: Character Stats Disp
Welcome MessageDeclare Character Stats9Functions Basics
Declaring a FunctionCalling a FunctionFunctions with ParametersFunctions with Multiple ParamsThe 'return' StatementRecap - Area Calculator