Separating string into bits

How could I for example, Hello World I like tacos and then split up all the words that I plan to later insert into a table

Use string.split() which will give you a table

local String = "Hello World I like tacos"
local Table = String:split(" ")
print(Table) -- {"Hello", "World", "I", "like", "tacos"}
2 Likes

You can use string.split for this:

local bits = string.split(str, “ “)

This will return a table with the characters separated by “ “.

To insert the bits in to a table you could use a for loop:

local tableofbits = {}

for _, bit in pairs (bits) do
    table.insert(tableofbits, bit)
end

What’s the point of inserting it into a new table when split returns a table?

Just do:

local tableofbits = string.split(“ “)

If you read his question he’d like to insert them into a table. He could do this for multiple strings and want a table with all of the bits.

He just said he wanted to split the string into bits contained in a table, he didn’t mention anything about multiple strings. In the example you described, what you did would be perfectly fine. But don’t think that’s what he meant.

1 Like