.sub()
Published Sep 8, 2023
Contribute to Docs
The .sub()
function is used to extract a substring from a given string. The extracted substring is determined by the integers passed to the function, which specify the starting and ending index.
Syntax
string.sub(originalString, startIndex, endIndex)
originalString
: The string from which the substring will be extracted.startIndex
: The index at which the substring should start (inclusive).endIndex
(optional): The index at which the substring should end (exclusive). IfendIndex
is not provided, the substring will extend to the end of the original string.
Example
Here are some examples of how to use string.sub()
:
local originalString = "Hello, World!"local subString1 = string.sub(originalString, 1, 5)local subString2 = string.sub(originalString, 8)print(subString1)print(subString2)
This will return the following output:
HelloWorld!
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Lua on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Lua
Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.Beginner Friendly4 hours