Joakim Krassman
Joakim Krassman

Reputation: 61

string.sub in Corona Lua crashes with ÅÄÖ

this snippet crashes my simulator bad.

s = "stämma"
s1 =  string.sub(s,3,3)
print(s1)

It seems like it handles my character as nil, any ideas?

Joakim

Upvotes: 0

Views: 399

Answers (1)

prapin
prapin

Reputation: 6877

I assume you are using UTF-8 encoding.

In UTF-8, a character can have a variable number of bytes, between 1 to 4. The "ä" character (228) is encoded with the two bytes 0xC3 0xA4.

The instruction string.sub(s, 3, 3) returns the third byte from the string (0xC3), and not the third character. As this byte alone is invalid UTF-8, Corona can't display the character.

See also Extract the first letter of a UTF-8 string with Lua

Upvotes: 3

Related Questions