For example:
local a = "Lua"
local u = "Луа"
print(a:len(), u:len())
output:
3 6
How can I output number of characters in utf-8 string?
If you need to use Unicode/UTF-8 in Lua, you need to use external libraries, because Lua only works with 8-bit strings. One such library is slnunicode. Example code how to calculate the length of your string:
local unicode = require "unicode"
local utf8 = unicode.utf8
local a = "Lua"
local u = "Луа"
print(utf8.len(a), utf8.len(u)) --> 3 3
In Lua 5.3, you can use utf8.len
to get the length of a UTF-8 string:
local a = "Lua"
local u = "Луа"
print(utf8.len(a), utf8.len(u))
Output: 3 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With