Logo

Programming-Idioms

History of Idiom 49 > diff from v26 to v27

Edit summary for version 27 by Nepta:
New Lua implementation by user [Nepta]

Version 26

2016-09-27, 15:57:17

Version 27

2016-11-05, 03:16:00

Idiom #49 Split a space-separated string

Build list chunks consisting in substrings of input string s, separated by one or more space characters.

Illustration

Idiom #49 Split a space-separated string

Build list chunks consisting in substrings of input string s, separated by one or more space characters.

Illustration
Code
chunks = {}
for substring in s:gmatch("%S+") do
   table.insert(chunks, substring)
end
Comments bubble
create an iterator (s.gmatch function) that match everything except the delimiter
Doc URL
http://www.lua.org/manual/5.3/manual.html#pdf-string.gmatch
Origin
http://lua-users.org/wiki/SplitJoin
Demo URL
http://codepad.org/SomCSysg