Converting string to array [Sloved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
zalander
Citizen
Posts: 72
Joined: Mon Jan 09, 2023 5:58 am
Location: India

Converting string to array [Sloved]

Post 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
Last edited by zalander on Sat Dec 09, 2023 8:58 am, edited 1 time in total.
Using LOVE to make everything except games :crazy:

Code: Select all

astring = "pog"
print(astring)
--pog
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Converting string to array

Post 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.
Last edited by dusoft on Sat Dec 09, 2023 1:03 pm, edited 1 time in total.
User avatar
BrotSagtMist
Party member
Posts: 614
Joined: Fri Aug 06, 2021 10:30 pm

Re: Converting string to array

Post by BrotSagtMist »

You probably want to use utf8.charpattern instead "." to have it catch the ö in löve.
obey
User avatar
zalander
Citizen
Posts: 72
Joined: Mon Jan 09, 2023 5:58 am
Location: India

Re: Converting string to array

Post 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
Using LOVE to make everything except games :crazy:

Code: Select all

astring = "pog"
print(astring)
--pog
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Converting string to array

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 66 guests