string.sub()
Part of the Fundamentals section of Coddy's Lua journey — lesson 46 of 90.
The string.sub() function allows you to extract a specific portion of a string, called a substring. This is useful when you need only part of a larger string, such as getting the first few characters of a name or extracting a specific section of text.
Here's the basic syntax:
string.sub(your_string, start_position, end_position)The function takes three arguments: the string you want to extract from, the starting position, and the ending position. Remember that in Lua, string positions start at 1, not 0.
Here's a simple example:
local word = "programming"
local first_three = string.sub(word, 1, 3)
print(first_three) -- outputs: proIn this example, string.sub() extracts characters from position 1 to position 3, giving us "pro". The original string remains unchanged - the function returns a new string containing just the extracted portion.
Challenge
EasyCreate a game character name extractor that retrieves the first part of a player's full character name. Declare a variable called characterName and assign it the value "Shadowblade the Mighty". Use the string.sub() function to extract the first 11 characters from the character name and store the result in a variable called shortName. Print the extracted short name.
Cheat sheet
The string.sub() function extracts a substring from a string:
string.sub(your_string, start_position, end_position)String positions start at 1 in Lua. The function returns a new string without modifying the original:
local word = "programming"
local first_three = string.sub(word, 1, 3)
print(first_three) -- outputs: proTry it yourself
-- Declare the character name variable
local characterName = "Shadowblade the Mighty"
-- TODO: Write your code here to extract the first 11 characters using string.sub()
-- Print the result
print(shortName)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