Lua string.format using UTF8 characters

Viewed 4504

How can I get the 'right' formatting using string.format with strings containing UTF-8 characters?

Example:

local str = "\xE2\x88\x9E"
print(utf8.len(str), string.len(str))
print(str)
print(string.format("###%-5s###", str))
print(string.format("###%-5s###", 'x'))

Output:

1   3
āˆž
###āˆž  ###
###x    ###

It looks like the string.format uses the byte length of the infinity sign instead of the "character length". Is there an UTF-8 string.format equivalent?

2 Answers
Related