Page 1 of 1

Converting string to array [Sloved]

Posted: Fri Dec 08, 2023 3:33 pm
by zalander
Hey !
Im making some dumb thing... again welp
I ran into a problem
I need to convert a string to a array

Code: Select all

a = "This is a string :)"
b = {"T", "h", "i", "s"} --[[ you get the idea]]--
I need to get a single character in a single space
Every help is appreciated :D

Re: Converting string to array

Posted: Fri Dec 08, 2023 4:09 pm
by dusoft
Iterate over the length of string and extract substring for each position that you insert into a table.

Or:

Code: Select all

str:gsub(".",function(c) table.insert(t,c) end)
(https://stackoverflow.com/questions/204 ... g-to-table)

BTW, Stackoverflow is better place to search for generic lua answers.

Re: Converting string to array

Posted: Fri Dec 08, 2023 4:14 pm
by BrotSagtMist
You probably want to use utf8.charpattern instead "." to have it catch the ö in löve.

Re: Converting string to array

Posted: Fri Dec 08, 2023 4:28 pm
by zalander
dusoft wrote: Fri Dec 08, 2023 4:09 pm Iterate over the length string and extract substring for each position that you insert into a table.

Or:

Code: Select all

str:gsub(".",function(c) table.insert(t,c) end)
(https://stackoverflow.com/questions/204 ... g-to-table)

BTW, Stackoverflow is better place to search for generic lua answers.
Thanks :D

Re: Converting string to array

Posted: Sat Dec 09, 2023 1:03 pm
by dusoft
BrotSagtMist wrote: Fri Dec 08, 2023 4:14 pm You probably want to use utf8.charpattern instead "." to have it catch the ö in löve.
Good point.