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.


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). If endIndex is not provided, the substring will extend to the end of the original string.


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)

This will return the following output:


All contributors

Looking to contribute?

Learn Lua on Codecademy