If the title does not explain clearly enough, my doubt is this: How can (If we can) we convert a string into a table? For example, I have a string value. The string is "MyStringvalue". Keep in mind that it's the STRING, not the NAME OF THE STRING. So now, in my table, I want the first term in the table to be "M". The second is "y". Case sensitive. The third will be "S" and so forth. Is there a way we can do this? And if so, how?
There's a few ways of doing it:
local myString = "Hello World" local characters = {} --putting string in table by looping through each position in the string --and getting the substring at that position (the character at that position for i = 1, #myString do table.insert(characters, myString:sub(i,i)) end --print out the characters array to show that we put the string in the table: for _, character in pairs(characters)do print(character) end
--use string.split("") to split up every character and put it into a table local myString = "Hello World" local characters = myString:split("") --print out the characters array to show that we put the string in the table: for _, character in pairs(characters)do print(character) end
Both ways work, but I prefer string.split(myString,"")
(approach two) since it's faster to write out.
Both have same speeds really.