dam1ne
dam1ne

Reputation: 361

Concatenation of string arrays in Lua

I have a string array named a

a = {"hi", "hello"}

and I'm trying to concatenate it into a string like this

require(2788315378).load(".. a",{"1w4q"})

I'm pretty sure this isn't the right way to do this, can you help?

Upvotes: 6

Views: 6599

Answers (1)

Corsaka
Corsaka

Reputation: 422

As @EgorSkriptunoff said, table.concat is the optimal way to do this.

table.concat(table [, sep [, i [, j]]])

It takes 1-4 parameters, the table, the separator, the ith element to start from, and the jth element to end on. Only the table is required.

Examples:

t1 = {"12","34","56"}
t2 = {"6","7","8"}
t3 = {"adsfa","important","bits","dfasdgf"}

print(table.concat(t1), --returns "123456"
table.concat(t2," and "), --returns "6 and 7 and 8"
table.concat(t3," ",2,3)) --returns "important bits"

It's important to note that you need a separator of some kind before adding i and j, even if that separator is just "".

Upvotes: 8

Related Questions