码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-19 12:44:53      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

 

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))

 

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

标签:

原文地址:http://www.cnblogs.com/dotdog/p/4513766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!