Sorry for the bad title, didn't know how to explain properly. I basically have a string like this
astring = "100, 200, 300, 400"
and I want to make this not a string, just 100, 200, 300, 400 (I'm trying to use string.char but I need it to be a number not a string and tonumber errors for me) how would I do this? thanks!
I would probably use gmatch which uses a pattern to match. %d+
means any digit matched one or more times.
Example:-
local tmp = "100, 200, 300, 400" for number in tmp:gmatch('%d+') do print(number) -- can then convert tonumber ect end
I hope this helps.
Using this website:
function split(str, pat) local t = {} local fpat = "(.-)" .. pat local last_end = 1 local s, e, cap = str:find(fpat, 1) while s do if s ~= 1 or cap ~= "" then table.insert(t,cap) end last_end = e+1 s, e, cap = str:find(fpat, last_end) end if last_end <= #str then cap = str:sub(last_end) table.insert(t, cap) end return t end parts = split("100, 200, 300, 400", '[, ]+') print(table.concat(parts, " "))
Comment if you need clarification.