To get the length in characters for UTF8 strings, do not use the # operator.
The length operator # gets the string length in bytes.
The length operator # gets the string length in bytes.
string.ulen(str)
Gets the length in characters of the provided UTF8 string.
Parameters
str
The UTF8 string whose length will be returned
Return value
Returns the number of characters.Example
-- declare an UTF8 string (file must be saved with UTF8 encoding)
local summer_in_french = "été"
-- outputs 5 (in bytes)
print( #summer_in_french )
-- outputs 5 (in bytes)
print( string.len(summer_in_french) )
-- outputs 3 (in characters)
print( summer_in_french:ulen() )