Hey,
Exactly what the title suggests.
Here is what I’m trying to achieve:
local t = "{213, workspace, 'hello'}"
print(t[1])
How would I go about removing the quotation marks (The function I use returns string and theres no way to change it)
Hey,
Exactly what the title suggests.
Here is what I’m trying to achieve:
local t = "{213, workspace, 'hello'}"
print(t[1])
How would I go about removing the quotation marks (The function I use returns string and theres no way to change it)
You can use string.split(s, ", ")
But it won’t remove the {
Something like this:
local s = "213, workspace, 'hello'" --Your string table without the brackets.
local tab = string.split(s, ", ") --Seperate every ,
But it’ll return a string table, so to use numbers you can use tonumber(s[1]). However, I am not sure about the workspace, maybe you can use game:GetService(s[2]), and s[2] is the service name.
local t = "{213, workspace, 'hello'}"
local Split = string.split(t, ", ")
Split[1] = Split[1]:sub(2)
Split[#Split] = string.sub(Split[#Split], 1, string.len(Split[#Split])-1)
What if I had a table like this:
{
Test1 = 1,
Test2 = "Hello",
Test3 = workspace
}
That’s a bit more complicated, I would split the , first, then split each one of them in the = , hold on, I’ll write an example:
Edit:
Here’s my example:
local strTab = "{Test1 = 1, Test2 = 'Hello', Test3 = Workspace}" --The string table
local realTab = {} --The real table
strTab = string.gsub(strTab, "{","") --Remove the {
strTab = string.gsub(strTab, "}","")--Remove the }
local tab2 = string.split(strTab, ", ") --Split every ,
for i = 1,3,1 do
local tab3 = string.split(tab2[i], " = ") --Split it on =
tab3[2] = string.gsub(tab3[2], "'", "") --Remove the '' of the string if there are any
if(tonumber(tab3[2])) then --If it's a number
realTab[tab3[1]] = tonumber(tab3[2])
elseif(game:FindFirstChild(tab3[2])) then --If it's in the game (service)
realTab[tab3[1]] = game:GetService(tab3[2])
else --If it's none of the above, it's probably a string
realTab[tab3[1]] = tab3[2]
end
end
print(realTab) --Print the table
@happygeneral2015 After some trying, I managed to get it working, I basically split it 2 times, one time for the , to have every object in the table separated, and second time the = to get the Index [“Test1”] and the value 1. And then I just checked what it is (string, number, service etc).
Here.
You can do that using 2 methods.
1: Manually converting table to string.
2: Using httpservice.
-- method 1
-- manually converting a simple table into a string
local function tabletostring(array)
local content = "{\n";
for i,v in pairs(array) do
local str = false;
if typeof(v):lower() == "string" then
str = true;
end
i,v = tostring(i),tostring(v);
if not str then
content = content..i.." = "..v.."\n";
else
content = content..i..' = "'..v..'"\n';
end
end
content = content.."}";
return content;
end
local t = {
Test1 = 1,
Test2 = "Hello",
Test3 = workspace
}
local strtable = tabletostring(t);
print(strtable);
--[[
OUTPUT:
{
Test3 = Workspace
Test1 = 1
Test2 = "Hello"
}
]]
-- method 2
-- converting table into a string using httpservice
local http = game:GetService("HttpService");
local cooltable = {
Test1 = 1,
Test2 = "Hello",
Test3 = workspace
}
local str = http:JSONEncode(cooltable);
print(str);
--[[
OUTPUT:
{"Test2":"Hello","Test1":1,"Test3":null}
]]
If you have any more questions, feel free to ask!