好记性不如铅笔头

C && C++, Lua, 编程

Lua快速学习笔记:值与类型

CONTENTS

备注:

1 本笔记只记录了LUA的一小部分内容,对于LUA的描述并不全面,以后随用随增加吧。
2 本笔记参考《Lua程序设计 第二版》,截图和代码属于原作者所有。
3 作者初学LUA,经验和能力有限,笔记可能有错误,还请各位路过的大牛们给予指点。

注释:

-- this is commment
--[[
	this is commment
--]]
注意这里的=的数目需要相等
--[==[
	this is commment
--]==]

变量:

访问未定义变量返回nil。

print(x);   -->输出nil
x = 10;
print(x);   -->输出10
x = nil;

类型:

关于bool值:

字符串:

LUA的字符串是不可变的值,LUA修改字符串会创建一个新的字符串。
LUA能够高效的处理长字符串。

关于单双引号,以下代码均可正确输出:

str = "Hello World";
print(str);
str = 'Hello World';
print(str);
str = 'Hello "test" World';
print(str);
str = "Hello 'test' World";
print(str);
str = "Hello 'test1' \"test2\" World";
print(str);
str = 'Hello \'test1\' "test2" World';
print(str);
str = "Hello \'test1\' \"test2\" World";
print(str);
str = 'Hello \'test1\' \"test2\" World';
print(str);

输出一段字符串,这里会精确的输出里面的所有内容,忽略其中的转义序列:

str = [[
aa;
bb;
"test1";
'test2';
\"test1\";
\'test1\';
]];
print(str);

输出为:

aa;
bb;
"test1";
'test2';
\"test1\";
\'test1\';

输出一段含有[[]]的字符串:

str = [===[
--this is commment1
--[[
	this is commment2
--]]
[[ ]]
[=[ ]=]
]===];
print(str);

输出:

--this is commment1
--[[
	this is commment2
--]]
[[ ]]
[=[ ]=]

字符串操作:

..:字符串连接符
#:获取字符串长度操作符

str = "Hello World";
print(str .. " length:" .. #str);       -->输出 Hello World length:11

发表评论

14 + 18 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据