简体   繁体   中英

Lua : Incorrect string length for String with Special Characters

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM