I have always wanted to know how to do this, if it is possible please tell me with an explanation on how it works
How To Split A String Into Its Individual Letters
Started by jay5476, Jul 23 2013 04:30 AM
4 replies to this topic
#1
Posted 23 July 2013 - 04:30 AM
#2
Posted 23 July 2013 - 04:48 AM
I had troubles understanding it at first then once I got the hang of string.* () (from the lua handbook) I couldn't understand why I had so much trouble with it. 
The above code will simply print each character to the screen.
instead of print(...) you could use a table and insert each character into a table, eg. tabletxt = string.sub(text,a,a)
edit :: oopsy, I did not miss a , while typing too fast. ( looks around and checks if anyone is looking and corrects the typo. ) ** stealths away into the sunshine **
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
The above code will simply print each character to the screen.
instead of print(...) you could use a table and insert each character into a table, eg. tabletxt = string.sub(text,a,a)
edit :: oopsy, I did not miss a , while typing too fast. ( looks around and checks if anyone is looking and corrects the typo. ) ** stealths away into the sunshine **
Edited by albrat, 23 July 2013 - 06:04 AM.
#3
Posted 23 July 2013 - 05:25 AM
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:
By the way:You miss a comma on the third line after "text".
EDIT: AND string.len(text) + 1 is totally nothing, since it's the n+1st character of an n-long string. Use "#text"
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
By the way:
albrat, on 23 July 2013 - 04:48 AM, said:
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
EDIT: AND string.len(text) + 1 is totally nothing, since it's the n+1st character of an n-long string. Use "#text"
#4
Posted 23 July 2013 - 06:06 AM
I origionally typed it as #text but second guessed myself and changed it. lol (moments insanity) (extended version)
#5
Posted 23 July 2013 - 07:45 AM
LBPHacker, on 23 July 2013 - 05:25 AM, said:
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:
You could use Lua patterns for this as well:
local str = "something"
local result = {}
for letter in str:gmatch(".") do table.insert(result, letter) endeven easier
local function split(str) if #str>0 then return str:sub(1,1),split(str:sub(2)) end end
you don't have to define a table but you can just
local results={split("randomstring!!")}
if you want a table andlocal res1,res2,res3=split("hai")
if you want individual vars
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











