table.concat()
The table.concat()
function combines the values of a table into a single string, optionally using a specified separator between each value.
Syntax
table.concat(tableData, separator, start, end)
Parameters
- Name
tableData
- Type
- table
- Description
The table whose values will be concatenated into a string.
- Name
separator
- Type
- string|nil
- Description
Optional. A string used as a separator between the values. Defaults to an empty string if not provided.
- Name
start
- Type
- number|nil
- Description
Optional. The starting index for concatenation. Defaults to
1
.
- Name
end
- Type
- number|nil
- Description
Optional. The ending index for concatenation. Defaults to the length of the table.
Return
Returns a string that consists of the concatenated values of the specified table.
Description
The table.concat()
function is useful for creating a single string from multiple values stored in a table. If the separator
parameter is provided, it will be inserted between each value. The start
and end
parameters allow for specifying a subrange of the table to concatenate.
Examples
Concatenating without a separator
local fruits = {"Apple", "Banana", "Cherry"}
print(table.concat(fruits))
-- Output: AppleBananaCherry
Concatenating with a separator
local fruits = {"Apple", "Banana", "Cherry"}
print(table.concat(fruits, ", "))
-- Output: Apple, Banana, Cherry
Specifying a range
local numbers = {10, 20, 30, 40, 50}
print(table.concat(numbers, "-", 2, 4))
-- Output: 20-30-40
Concatenating with default parameters
local words = {"Hello", "World"}
print(table.concat(words, " "))
-- Output: Hello World
See also
table.insert()
: Inserts a value into a table at a specified position.table.remove()
: Removes a value from a table at a specified position.