The Length Operator(井符号) 和 string.len操作符

 

2.5.5 – The Length Operator(#)

The length operator is denoted by the unary(一元的) operator #. The length of a string is its number of bytes (that is, the usual meaning of string length when each character is one byte). The length of a table t is defined(定义) to be any integer(整数) index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero. For a regular array(数组), with non­nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has "holes" (that is, nil values between other nonnil values), then #t can be any of the indices(指数) that directly precedes(领先) a nil value (that is, it may consider any such nil value as the end of the array(数组)).

 

string.len (s)
Receives a string and returns its length. The empty string "" has length 0. Embedded zeros are counted, so
"a\000bc\000" has length 5.


 

示例:

 

local x = "string"
local tableDemo = {
    "helloworld",
    "appp",
    nil,  --holes
    "app",
    "appx",
    nil,  --holes
    'x'
}

print(#x)
print(#tableDemo)

print(string.len(x))
-- print(string.len(tableDemo))

 

posted on 2015-05-19 10:01  dotdog  阅读(258)  评论(0编辑  收藏  举报

导航