Jump to content




How To Split A String Into Its Individual Letters


  • You cannot reply to this topic
4 replies to this topic

#1 jay5476

  • Members
  • 289 posts

Posted 23 July 2013 - 04: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

#2 albrat

  • Members
  • 162 posts
  • LocationA Chair

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. :D
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 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

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:
local str = "something"
local result = {}
for letter in str:gmatch(".") do table.insert(result, letter) end

By the way:

View Postalbrat, 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
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"

#4 albrat

  • Members
  • 162 posts
  • LocationA Chair

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 KaoS

    Diabolical Coder

  • Members
  • 1,510 posts
  • LocationThat dark shadow under your bed...

Posted 23 July 2013 - 07:45 AM

View PostLBPHacker, 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:
local str = "something"
local result = {}
for letter in str:gmatch(".") do table.insert(result, letter) end

even 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 and
local res1,res2,res3=split("hai")
if you want individual vars





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users