Split a String into Numbers using Separators (code snippet)


Various LUA and IUP features supply a series of numbers as a string with separators such as space or comma or 'x'.
e.g. "123 654 789" or "650,320" or "75x80"

This snippet splits the string and returns the numbers in an array table. If any element is not a number then its string value is returned.

Requires: None


-- Split a string into numbers using separators space or comma or x --
function string.splitnumbers(strTxt)
	local tblNum = {}
	strTxt = tostring(strTxt or "")
	strTxt:gsub("([^ ,x]+)", function(strNum) tblNum[#tblNum+1] = tonumber(strNum) or strNum end)
	return tblNum
end -- function string.splitnumbers


local tblScrn = iup.GetGlobal("VIRTUALSCREEN"):splitnumbers()
print( tblScrn[1], tblScrn[2], tblScrn[3], tblScrn[4] )		-->>	0	0	1600	900