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?
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
%S represents all non-space characters.
Commented
May 6, 2010 at 15:45
s = "foo bar 123"
words = {}
for word in s:gmatch("%w+") do table.insert(words, word) end
%w represents all alphanumeric characters.
Commented
May 6, 2010 at 15:44
[a-zA-Z0-9]+ which would not include _. You'd probably want to use the answer provided by @lhf.
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"