sub function doesn't work when myString own "é", "è", "ê","â".

Hi,

I need to get all the characters of a string in a tab.

My string can own characters like “é”, “è”, “ê”,“â”

At this point I code this :

local myStringLength = string.len(myString) local stringTab = {} for i = 1, myStringLength do --get each character local currentCharacter = myString:sub(i,i) --insert each letter in a tab table.insert(stringTab, currentCharacter) end

My problem is that sub function doesn’t work when myString own “é”, “è”, “ê”,“â”.

What can I do to fix that?

Any help would be really appreciated

Thank you

Hi @oromanetti,

Most likely, this is because the length of the string in Lua is “longer” than you think because non-UTF characters are multi-byte, and so it’s not going on a 1-to-1 basis. Perhaps you can determine how many bytes these special characters take, then search for them within the string, and if found, use sub() to take out that number of characters from the string and insert into the other table.

Here’s some reference:

http://docs.coronalabs.com/api/library/string/len.html

Best regards,

Brent

This module do what you need.

https://github.com/Pogs/lua-utf8-simple

Thanks Fernandomgf :slight_smile:

Hi @oromanetti,

Most likely, this is because the length of the string in Lua is “longer” than you think because non-UTF characters are multi-byte, and so it’s not going on a 1-to-1 basis. Perhaps you can determine how many bytes these special characters take, then search for them within the string, and if found, use sub() to take out that number of characters from the string and insert into the other table.

Here’s some reference:

http://docs.coronalabs.com/api/library/string/len.html

Best regards,

Brent

This module do what you need.

https://github.com/Pogs/lua-utf8-simple

Thanks Fernandomgf :slight_smile: