Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to turn this string into not-a-string?

Asked by
trecept 367 Moderation Voter
6 years ago

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!

0
Do you want to turn this into a number table? Zenith_Lord 93 — 6y
0
to be specific i'm creating a plugin and using string.gsub, string.char etc, i'm changing the sources of scripts and I need to be able to do string.char(100, 200, 300 ,400) instead of string.char(astring) which is "100, 200, 300 ,400" trecept 367 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago

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.

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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.

Answer this question