tags:

views:

177

answers:

2

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?

Thanks!

+7  A: 
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
lhf
reference manual gnome says: `%S` represents all non-space characters.
kaizer.se
The accepted answer (ponzao) is ok with the specification in the question, but the reason to prefer lhf's answer is that if you have 8-bit or multibyte text (everything non-ascii), you can still split correctly on only spaces using this method.
kaizer.se
+3  A: 
s = "foo bar 123"
words = {}
for word in s:gmatch("%w+") do table.insert(words, word) end
ponzao
reference manual gnome says: `%w` represents all alphanumeric characters.
kaizer.se