string.rep()
Part of the Fundamentals section of Coddy's Lua journey — lesson 47 of 90.
The string.rep() function allows you to repeat a string a specified number of times. This is particularly useful for creating visual elements like separator lines, borders, or patterns in your output.
Here's the basic syntax:
string.rep(your_string, number_of_repetitions)The function takes two arguments: the string you want to repeat and the number of times to repeat it. Here's a simple example:
local separator = string.rep("-", 10)
print(separator) -- outputs: ----------In this example, the dash character is repeated 10 times to create a line. This technique is commonly used to create visual separators in console output, making your program's display more organized and readable.
Like other string functions, string.rep() returns a new string containing the repeated pattern - it doesn't modify the original string.
Challenge
EasyCreate a visual border generator for a game menu using the string.rep() function. Declare a variable called borderChar and assign it the value "*". Use string.rep() to repeat this character 15 times and store the result in a variable called menuBorder. Print the generated border.
Cheat sheet
The string.rep() function repeats a string a specified number of times:
string.rep(your_string, number_of_repetitions)Example creating a separator line:
local separator = string.rep("-", 10)
print(separator) -- outputs: ----------The function returns a new string containing the repeated pattern without modifying the original string.
Try it yourself
-- Declare variables
local borderChar = -- TODO: Assign the border character
local menuBorder = -- TODO: Use string.rep() to repeat the character 15 times
-- TODO: Write your code here
-- Output the result
print(menuBorder)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