Reputation: 42627
I have a string in lua.
It's a bunch of [a-zA-Z0-9]+ separated by a number (1 or more) spaces.
How do I take the string and split it into a table of strings?
Upvotes: 12
Views: 41051
Reputation: 521
Here is an example how to split words and merge them:
function mergeStr (strA, strB)
local tablA, tablB = {}, {}
for word in strA:gmatch("%S+") do
table.insert (tablA, word)
end
for word in strB:gmatch("%S+") do
table.insert (tablB, word)
end
return tablA[1] .. ' ' .. tablB[3] .. ' ' .. tablA[5]
end
print (mergeStr ("Lua is a programming language", "I love coding"))
-- "Lua coding language"
Upvotes: 0
Reputation: 20934
s = "foo bar 123"
words = {}
for word in s:gmatch("%w+") do table.insert(words, word) end
Upvotes: 20
Reputation: 72312
s="How do I take the string and split it into a table of strings?"
for w in s:gmatch("%S+") do print(w) end
Upvotes: 48