Question

For an example, lets say I have this table:

tbl = {"hi ", "my ", "name ", "is ", "King"}

Can I get this to return:

"hi my name is King"

Without

for k, v in ipairs( tbl )
  print(v)
end

Because I am trying to process an unknown quantity of inputs, and compare the result to another string.

Was it helpful?

Solution

You can use table.concat() to get the result string:

local str = table.concat(tbl)
print(str)

It can do more, in particular, table.concat() takes a second optional parameter, which can be used as the separator, for instance, to use commas to separate each elements:

local str = table.concat(tbl, ',')

The biggest advantage of table.concat() versus direct string concatenation is performance, see PiL §11.6 for detail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top