Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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!

2 comments

0

Do you want to turn this into a number table?

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"

2 answers

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.