Lua Programming/How to Lua/length operator

From Wikibooks, open books for an open world
< Lua Programming‎ | How to Lua
Jump to: navigation, search

Length operator [edit]

The hash symbol can be used as a length operator for obtaining the length of a string or table.

Length of a string [edit]

print (#"oranges")    -- 7

Note that in lua, the string library provides a string.len function, which can also be used to determine the length of a string:

print (string.len("oranges"))    -- 7

Length of a table [edit]

For a regular array with numerical indices, the length operator returns the number of the last populated element. However, if the array contains a nil value, this may be interpreted as the end of the array by the length operator: