Lua Programming/How to Lua/length operator

Length operatorEdit

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

Length of a stringEdit

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 tableEdit

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:

Last modified on 17 June 2011, at 19:14