concat()
Anonymous contributor
Published Oct 26, 2023
Contribute to Docs
The concat()
function is used to join the values of a table into a single string, separated by a specified separator. This function is particularly valuable when a table needs to be converted into a formatted string representation. The method allows for the use of a custom separator between values and the option to specify a range of indices to concatenate.
Syntax
concat(table, separator, [i], [j])
Parameters
concat()
has the following parameters:
table
: The table containing values to concatenate.separator
: The character or string used to separate the concatenated values.[i]
(optional): The starting index in the table for concatenation.[j]
(optional): The ending index in the table for concatenation.
Example
In this example, the fruits
table contains four elements. table.concat()
is used to join the elements from index 2 to 3 (inclusive) with a comma and space as the separator. The resulting string is then printed.
local fruits = {"apple 🍎", "banana 🍌", "cherry 🍒", "watermelon 🍉"}local result = table.concat(fruits, ", ", 2, 3)print(result)
The output is the following:
banana 🍌, cherry 🍒
All contributors
- Anonymous contributor
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