2.lua的类型和值

--------------------------2.1-类型和值---------------------------------------------


--8个基本类型:nil boolean number string userdata function thread table


--[[
print(type("Hello World")) -->string
print(type(10.4*3)) -->number
print(type(print)) -->function
print(type(type)) -->function
print(type(true)) -->boolean
print(type(nil)) -->nil
print(type(type(x))) -->string


----------------------------------------------------


print(type(a)) -->nil
a=10
print(type(a)) -->number
a="a string"
print(type(a)) -->string
a=print
print(type(a)) -->function
--]]


----------------------------------------------------


--[[
a = "one string";
b = string.gsub(a,"one","another")
print(a) -->one string
print(b) -->another string
--]]


----------------------------------------------------


--转义符--
--[[
\a bell
\b back space --后退
\f form feed --换页
\n newline --换行
\r carriage return --回车
\t horizontal tab --制表
\v vertical tab
\\ backslash --"\"
\" double quote --双引号
\' single quote --单引号
\[ left square bracket --左中括号
\] right square bracket --右中括号
--]]
-----------------------------------------------------


--print("one line\nnext line\n\"inquotes\",'in quotes'")


--[[
运行结果:
one line
next line
"inquotes",'in quotes'
--]]




--print('a backslash inside quotes:\'\\\'') -->a backslash inside quotes:'\'


--print("a simpler way: '\\'") -->a simpler way: '\'


------------------------------------------------------------


--使用[[...]]表示字符串--


page=[[
<HTML>
<HEAD>
<TITLE>An HTML Page</TITLE>
</HEAD>
<BODY>
Lua
a text between double brackets
</BODY>
</HTML>
]]


io.write(page)




--------------------------------------------------------
--[[
print("10"+1) -->11
print("10+1") -->10+1
print("-5.3e-10"*"2") -->-1.06e-009
--print("hello"+1) -->ERROR(cannot convert "hello")
print(10 .. 20) --1020
--]]
---------------------------------------------------------


--[[
line = io.read()
n=tonumber(line)
if n==nil then
error(line .. " is not a valid number")
else
print(n*2)
end


--]]


print(tostring(10)=="10") -->true
print(10 .. "" == "10") -->true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值