Lua - convert string to table

user3074258 picture user3074258 · Dec 6, 2013 · Viewed 38.4k times · Source

I want to convert string text to table and this text must be divided on characters. Every character must be in separate value of table, for example:

  • a="text"
  • --converting string (a) to table (b)
  • --show table (b)
  • b={'t','e','x','t'}

Answer

moteus picture moteus · Dec 6, 2013

You could use string.gsub function

t={}
str="text"
str:gsub(".",function(c) table.insert(t,c) end)