I want to create 1, specific string and then put them into the array like this:
1 | local myString = "example string" |
and then get in result:
1 | local myList = { "e" , "x" , "a" , "m" , etc. } |
you use string.gmatch()
and use the "." string pattern, which is every character inside a string. then just literate over it and insert each character into a table.
1 | local myString = "example string" |
2 | local myTable = { } |
3 |
4 | for character in string.gmatch(myString, "." ) do |
5 | table.insert(myTable, character) |
6 | end |
then if you want, you can literate over the table and print each character or value.
1 | for _,v in pairs (myTable) do |
2 | print (v) -- just prints the value or the character |
3 | end |
This is easily done with just string.sub and a basic for loop.
1 | function getChars(InputString) |
2 | local ret = { } |
3 | for local i = string.len(InputString), 1 , - 1 do -- For every character |
4 | ret [ i ] = string.sub(InputString,_,_) -- Add this character to the table |
5 | end |
6 | return ret |
7 | end |