string.len()
Part of the Fundamentals section of Coddy's Lua journey. Lesson 44 of 90.
The string.len() function is your first tool for working with text data in more sophisticated ways.
The string.len() function returns the number of characters in a string. This includes letters, numbers, spaces, and any special characters. Here's the basic syntax:
string.len(your_string)Here's a simple example:
local username = "player123"
local length = string.len(username)
print(length) -- outputs: 9The string "player123" contains 9 characters, so string.len() returns 9.
Challenge
EasyCreate a username validation system that checks if a username meets the minimum length requirement. Declare a variable called username and assign it the value "player123". Use the string.len() function to find the length of the username, then store this length in a variable called usernameLength. Finally, print the length value.
Try it yourself
-- Declare the username variable
local username = "player123"
-- TODO: Write your code here to find the length and store it in usernameLength
-- Print the length
print(usernameLength)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 CalculatorPractice on your own: Online Lua compiler