Page 1 of 1

Number of characters in utf8 string

Posted: Sun Apr 10, 2022 2:04 pm
by moon69
Any suggestions on how to get the number of displayed characters in a lua string that may contain utf8 chars?

#str & string.len(str) return the number of bytes rather than number of characters.

There is the utf8 string module in lua 5.3, but it's not found in game (not sure if Factorio is still on lua 5.2.x or if it's disabled?).

I'm trying to force some display padding and sorting behaviours.

Re: Number of characters in utf8 string

Posted: Sun Apr 10, 2022 3:39 pm
by Qon
My first thought is to convert the string to . or A or some other known len() character with Lua patterns. Replace every character with the same and then measure length. Would be trivial in regex, but patterns must operate on the character level. Or look in documentation for a proper character length function if you don't want to make messy and bad code ;) .