Menu
Coddy logo textTech

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: pro

In 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 icon

Challenge

Easy

Create 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: pro

Try 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)
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals