Lua学习笔记三

3.1k 词

Lua中可以用三种方式定义字符串:

  • 单引号 ‘hello world’
  • 双引号 “heool world”
  • [[ ]] [[hello world]]

字符串操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

print(string.upper("hello world")) --HELLO WORLD

--string.lower(str)转小写
print(string.lower("HELLO WORLD")) --hello world

--[[string.gsub(mainStr,findStr,replaceStr,num)字符串替换,返回替换后的字符串和替换的次数
mainStr 整个需要被替换的字符串
findStr 要被替换的部分
replaceStr 新的字符串
要替换的数量,不填就是替换所有
--]]
print(string.gsub("aaaa","a","b",3)) --bbba 3

--string.find()在一个指定的目标字符串中搜索指定的内容(第三个参数为索引,从第几个开始查找),返回其具体位置(从开始的第一个)。不存在则返回 nil
print(string.find("hello world","o",6)) --结果 8 8

--string.reverse()字符串反转
print(string.reverse("hello"))

--string.format() 字符串格式化
print(string.format("the value is:%d",4)) --the value is:4

--string.char()转成字符串 string.byte() 转成整数值(指定某个位置,不知道默认第一个)
print(string.char(97,98,99,100)) -- abcd
print(string.byte("abcd")) --97
print(string.byte("abcd",2)) --98 第二个字母转成整数值

--string.len("abcd") 字符串长度
print(string.len("abcd")) --4
print(#"abcd") --4

--string.rep("",n) 字符串的n次拷贝
print(string.rep("abcd",2)) --abcdabcd

-- .. 字符串拼接
print("abc".."d") --abcd

--string.gmatch(str, pattern) 回一个迭代器函数,每一次调用这个函数,返回一个在字符串 str 找到的下一个符合 pattern 描述的子串
for world in string.gmatch("Hi hello world","%a+") do
print(world)
end
--[[ 结果
Hi
hello
world
--]]

--string.match(str, pattern, init)只寻找源字串str中的第一个配对. 参数init可选, 指定搜寻过程的起点, 默认为1。
print(string.match("I have 2 questions for you.", "%d+ %a+")) --2 questions

Lua数组

一维数组

Lua中数组索引从1开始

1
2
3
4
array = {"hello","world"}
for i=0,2 do
print(array[i])
end

结果:

1
2
3
nil
hello
world

可以以负数作为数组的索引:

1
2
3
4
array = {}
for i = -1,2 do
array[i] = i
end

多维数组

1
2
3
4
5
6
7
local array = {}
for i = 1,3 do
array[i] = {}
for j = 1,3 do
array[i][j] = i * j
end
end

迭代器

泛型for迭代器

1
2
3
for k,v in pairs(table) do
print(k,v)
end

泛型 for 在自己内部保存迭代函数,实际上它保存三个值:迭代函数、状态常量、控制变量

无状态的迭代器

无状态的迭代器是指不保留任何状态的迭代器

多状态的迭代器

可以通过闭包来实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
array = {"Google", "Runoob"}

function (collection)
local index = 0
local count = #collection
-- 闭包函数
return function ()
index = index + 1
if index <= count
then
-- 返回迭代器的当前元素
return collection[index]
end
end
end

for element in elementIterator(array)
do
print(element)
end

table

table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数组、字典等。

1
2
3
4
5
6
7
8
9
-- 初始化表
table = {}

-- 指定值
table[1]= "Lua"

-- 移除引用
table = nil
-- lua 垃圾回收会释放内存

table的操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
t = {"aaa","bbb","ccc"}
--[[
table.concat (t , sep , start , end)
将t的所有元素连接起来,用sep作为连接符
从start开始到end结束
sep,start,end可以不写
--]]
print("concat:",table.concat( t, ", ")) --aaa, bbb, ccc

--[[
table.insert (table, pos, value):
在t的pos位置插入value
pos可以省略,表示在末尾插入
--]]
table.insert(t,"ddd")
print("t[4]:",t[4]) --ddd

--[[
table.remove (t , pos)
删除t的pos位置上的元素
--]]
table.remove(t)
print("t[4]:",t[4]) --nil

--[[
table.sort (t , comp)
给t升序排序
comp可以省略
--]]
tt = {"ddd","fff","xxx","aaa"}
table.sort(tt)
for i,v in ipairs(tt) do
print(i,v)
end
--[[
1 aaa
2 ddd
3 fff
4 xxx
--]]

无论是#还是table.getn都会在索引中断的地方停止计数,所以要自己去table的长度

1
2
3
4
5
6
7
function table_leng(t)
local leng=0
for k, v in pairs(t) do
leng=leng+1
end
return leng;
end