Consider the two cases below:
local str1 = "abc"
str1:len gives 3
local str2 = "£££"
str2:len gives 6
Can someone explain this?
LuaJit version: 5.1
The length of strings in Lua is the number of bytes in it, not the number of chars.
To handle multibyte charsets, you need a library like utf8, which is available in Lua 5.3.
Found a solution.
local function parse_string(str)
local ret = ""
local flag = true
for i = 1, #str do
local c = str:sub(i,i)
local char = string.char(b2i.toint(c, "big", false, 1))
if char > "\127" then
flag = not flag
if(flag) then
ret = ret .. char
end
else
ret = ret .. char
end
end
return ret
end
local str2len = #str2:gsub("[\128-\191]", "")