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))
时间: 2024-08-10 05:36:58

The Length Operator(井符号) 和 string.len操作符的相关文章

lua 中string字符串的使用(string.len, string.char)

table.keys 返回指定表格中的所有键. 格式: keys = table.keys(表格对象) 用法示例: local t = {a = 1, b = 2, c = 3} local keys = table.keys(t) -- keys = {"a", "b", "c"} ~~ table.values 返回指定表格中的所有值. 格式: values = table.values(表格对象) 用法示例: local t = {a =

CREATE OPERATOR CLASS - 定义一个新的操作符类

SYNOPSIS CREATE OPERATOR CLASS name [ DEFAULT ] FOR TYPE data_type USING index_method AS { OPERATOR strategy_number operator_name [ ( op_type, op_type ) ] [ RECHECK ] | FUNCTION support_number funcname ( argument_type [, ...] ) | STORAGE storage_type

[转]Date and String Function in BluePrism

本文转自:https://www.codeproject.com/Articles/1247389/Date-and-String-Function-in-BluePrism This article appears in the Third Party Products and Tools section. Articles in this section are for the members only and must not be used to promote or advertise

Mycat sql预处理功能分析

前言 Mycat从1.6版本开始支持预处理.目前1.6分支还是开发测试阶段.Mycat发展自Cobar,在Cobar源码里面可以看到预处理功能的影子(未完全实现,当然你如果往Cobar上面调用预处理命令,那么Cobar会返回结果,告诉你,我是不支持预处理sql的!).我在以前的公司接到需求,需要在Mycat中实现预处理,所以花了时间研究了这部分代码并以自己的思路去实现.后面我将代码贡献给社区,在社区的帮助下,修复了一些bug,最终整合到1.6分支上. Mycat预处理的使用场景不多,针对java

leetcode 140. Word Break II ----- java

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. For example, givens = "catsanddog",dict = ["cat", "cats"

为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?

一般而言,实现"读入用户输入的字符串",程序中自然不能对用户输入的长度有所限定.这在C++中很容易实现,而在C中确没那么容易. 这一疑问,我在刚学C++的时候也在脑中闪现过:不过很快将它抛在脑后了.直到最近,我在百度知道上讨论一个单词统计问题(链接)时,才重新想起.于是,翻出gcc 4.6.1的代码,浏览了一番. 首先,明确这里探讨的场景--从标准输入(或字符模式打开的文件)中读取一个字符串(换行.空格.tab间隔均可).用C++实现这一功能有两种选择--使用C标准库和使用C++标准库

Leetcode: Encode String with Shortest Length && G面经

Given a non-empty string, encode the string such that its encoded length is the shortest. The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note: k will be a positive integ

Bash String Manipulation Examples – Length, Substring, Find and Replace--reference

In bash shell, when you use a dollar sign followed by a variable name, shell expands the variable with its value. This feature of shell is called parameter expansion. But parameter expansion has numerous other forms which allow you to expand a parame

Given a string, find the length of the longest substring without repeating characters.(给定一个字符串,找到最长的子串的长度,这个子串不存在重复的字符。 )

Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1.