Length


Lua includes two methods of determining the length of a string; the length operator # and the library function string.len.

Whereas similar functions in most high-level languages return the length as the number of characters in the string, Lua returns the number of bytes of memory occupied by the string. In many common cases such as ASCII these yield the same result, but case should be taken to watch for unexpected results.

Let's take a look at a few examples:

local x = "abcd"
local y = "ab cd"

print(#x) -- 4
print(#y) -- 5

print(string.len(x)) -- 4
print(y:len()) -- 5

In this case, we defined two strings containing only ASCII characters, and demonstrated how to find the length using both the length operator and the string.len library function. Both examples are basically the same, although the second example include a space (" ") to show that these are counted, just like any other character.

Now, let's take a look at a few unicode strings, which can each occupy multiple bytes:

local x = "中文"
local y = "zhōng wén"
local z = "😊"

-- careful!
print(#x) -- 6
print(#y) -- 11
print(#z) -- 4

print(string.len(x)) -- 6
print(y:len()) -- 11
print(z:len()) -- 4

These are (probably) not the results that we would have expected. Our main point is to make you aware of how Lua treats string length, and how it might produce unexpected results.