2

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.

1 Answer 1

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.