3000 explained code solutions for 75 technologies


luaHow to split string


Let's split a string by space:

str = "green yellow blue"
words = {}

for w in string.gmatch(str, "[^ ]+") do
   table.insert(words, w)
endctrl + c
str =

sample string to split

words

this array will container list of words

string.gmatch

finds all matches by a given regex

for w in

iterate through all matches items

"[^ ]+"

match all symbols except space (thus, we're going to split by space here)

table.insert(words, w)

append found match (word) to words array