Unfortunately, in LUA, there is no inbuilt string splitting function, which is very inconvenient. However, you could use string.gmatch to begin a regular expression matching and the following is a short and neat alternative.
A string splitting function will split a source string according to a delimiter (char separator), into a array of substrings. This usually requires string parsing.
1 2 3 4 5 6 7 | function split(s, delimiter) result = {}; for match in (s..delimiter):gmatch("(.-)"..delimiter) do table.insert(result, match); end return result; end |
function split(s, delimiter) result = {}; for match in (s..delimiter):gmatch("(.-)"..delimiter) do table.insert(result, match); end return result; end
The following tests it:
1 2 3 4 5 | s = split(io.read(), ",") for key, value in pairs(s) do print(key..'='..value) end |
s = split(io.read(), ",") for key, value in pairs(s) do print(key..'='..value) end
Unlike other programming language, the string concatenation is made possible by double dots “..”.
–EOF (The Ultimate Computing & Technology Blog) —
GD Star Rating
loading...
171 wordsloading...
Last Post: Coding Exercise - 1. Life, the Universe, and Everything - Learning LUA programming language
Next Post: Fasthosts Disable My website because WordPress sending too many emails due to spam comments