Posted 23 July 2013 - 05:30 AM
I have always wanted to know how to do this, if it is possible please tell me with an explanation on how it works
text = "this string is for splitting" -- our text
for a = 1, string.len(text)+1 do -- for 1 to the number of letters in our text + 1
print (string.sub(text, a,a)) -- split the string with the start and end at "a"
end -- repeat untill the for loop ends
local str = "something"
local result = {}
for letter in str:gmatch(".") do table.insert(result, letter) end
You miss a comma on the third line after "text".text = "this string is for splitting" -- our text for a = 1, string.len(text)+1 do -- for 1 to the number of letters in our text + 1 print (string.sub(text a,a)) -- split the string with the start and end at "a" end -- repeat untill the for loop ends
Overkill time!… (That means: Yes, I know the question's been answered already, but I feel like posting an answer anyways.)
You could use Lua patterns for this as well:local str = "something" local result = {} for letter in str:gmatch(".") do table.insert(result, letter) end
local function split(str)
if #str>0 then return str:sub(1,1),split(str:sub(2)) end
end
local results={split("randomstring!!")}
if you want a table and
local res1,res2,res3=split("hai")
if you want individual vars