3

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

1
  • 1
    local str2len = #str2:gsub("[\128-\191]", "") Commented Dec 21, 2017 at 13:18

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

the issue I am facing is I need to support a string of length 80. So input with characters as '£' are exceeding the length as per lua, but not from the users perspective.
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
@user1681485 If users understand anything about the "length" of text, it would be graphemes and whitespace between visible characters. An artificial limit on your internal representation of their text would be quite difficult to explain precisely.
An example of using utf8 library would be a great addition to this answer :)
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.