Search code examples
stringutf-8luasymbolscyrillic

Lua string length (cyrillic in utf8)


How to get the REAL length of the string with cyrillic symbols in lua?

If i'm using string.len("HELLO.") - I will get 6

But with string.len("ПРИВЕТ") - I will get 12(Same with "#" operator)

So the number of symbols didn't changed, but we got different numbers... It's because cyrillic symbols has two bytes, when english letters, numbers and etc has 1. I want to know how to get the right and real length of string(Get 6 in both samples). Everyone who can help?


Solution

  • string.len and # count bytes, not chars.

    In Lua 5.3+, use utf8.len.