1. 程式人生 > 其它 >H7-TOOL的LUA小程式教程第2期:變數,迴圈,函式,條件語句和字串相關API

H7-TOOL的LUA小程式教程第2期:變數,迴圈,函式,條件語句和字串相關API

LUA指令碼的好處是使用者可以根據自己註冊的一批API(當前TOOL已經提供了幾百個函式供大家使用),實現各種小程式,不再限制Flash裡面已經下載的程式,就跟手機安裝APP差不多,所以在H7-TOOL裡面被廣泛使用,支援線上除錯執行,支援離線執行。

TOOL的LUA教程爭取做到大家可以無痛呼叫各種功能函式,不需要學習成本。


掌握這些基礎就夠用了。

一、註釋

註釋、多行註釋、取消多行註釋:

註釋單行

--local a = 1

註釋多行

--[[
local a = 1
--]]

取消註釋多行

---[[
local a = 1
--]]


二、變數:



變數
1.變數無需宣告
2.變數沒宣告為nil,賦值為nil等同於刪除
3.lua把nil,false視為假,其他都為true
4.Lua中的變數全是全域性變數,除非用 local 顯式宣告為區域性變數
5.#ta字串的長度,也可以獲取表格數字索引對應的個數

例子

--全域性賦值
x = 1

--區域性多個賦值
local x,y = 1,2

local ta = {1,2,3,name='安富萊'}
--3
print(#ta)



三、條件:

條件
if
and or not
>= <= == > < ~=

例子

if (a >= 0) then   --大於等於
  b 
= b + 1 else b = b - 1 end if (a ~= 0) then --不等於 end if (a==b and c == 0) then --邏輯與 elseif (a~=b) then --不等於 elseif (not a) then --邏輯非 else end


四、迴圈

例子

local a=10
while (a < 20)
do
   a = a + 1
   print(a)
end

local t = {1,2,3}
for i,v in pairs(t) do
    print(v)
end


--每次加1

for
i=0,5,1 do print(i) end


--每次減1

--5,4,3,2,1
for i=5,1,-1 do
    print(i)
end



五、函式

例子:

--宣告函式,傳遞2個引數,返回2個值得和
function add(num1, num2)
   return num1 + num2
end


--把函式賦值給一個變數,然後可以當引數傳遞

local myprint = function(str)
   print(str)
end

function say(arg1,func)
    func(arg1)
end

--www.freecls.com
say('www.freecls.com',myprint)


--可變引數

function average(...)
   result = 0
   local arg={...}
   for i,v in ipairs(arg) do
      result = result + v
   end
   print("總共傳入 " .. #arg .. " 個數")
   return result/#arg
end
function what()
    return '安富萊','www.armlfy.com'
end

--返回多個引數
local name,url = what()
print(name,url)



、常用字串函式:

string.byte(s [, i [, j]])
string.byte是用來把字元轉換成ascii數字,s為目標字串,i為索引開始位置(從1開始),j為索引結束位置
例子

--預設為第1個返回a的ascii值
local r = string.byte('abcdefg') --97
--從索引2(b)到索引4(d)也就是分別返回bcd的ascii值
local r1,r2,r3 = string.byte('abcdefg',2,4) --98,99,100


string.char(...)
string.char是把ascii數值轉換成字元
例子

--返回98所對應的字元
local r = string.char(98) --a
--返回98,,99,100對應的字元並連在一起返回
local r = string.char(98,99,100) --abc


string.sub (s, i [, j])
擷取字串(字串分割,字串擷取),i為起始索引,可選引數j為結束索引(包含),都可以為負數,第一個字元索引為1,最後一個字元為-1
例子

local res,s
s = 'www.armfly.com'
res = string.sub(s,5)     --armfly.com
res = string.sub(s,5,-1)  --armfly.com

--擷取後3位
res = string.sub(s,-3)    --com

--擷取前3位
res = string.sub(s,1,3)   --www


string.find (s, pattern [, init [, plain]])
字串查詢函式找不到返回nil,找到了返回開始位置和結束位置,init為從哪裡開始預設為1,plain預設為false表示利用模式匹配,如果設為true則表示純文字匹配(也就是關閉正則匹配)
例子

local str = 'i love programming,11,22,%d+aa'
local s = string.find(str,'222')    --nil
s = string.find(str,'pro')  --8
s = string.find(str,",%d+")    --19(匹配到了,11)
s = string.find(str,",%d+",1,true) --25(由於關閉了模式匹配,所以匹配到了,%d+)


string.match (s, pattern [, init])
它跟string.find差不多,只不過能把捕獲匹配到的結果並返回
例子

local s,res,res1,res2
s = 'http://www.armfly.com'

--由於沒有捕獲,返回全部匹配
--結果:http://www.armfly.com
res = string.match(s,'http://%a+.%a+.com')

--如果有捕獲,則分別返回捕獲結果
--結果:www    armfly
res1,res2 = string.match(s,'http://(%a+).(%a+).com')


string.gsub (s, pattern, repl [, n])
用來做字串替換,可選引數n代表替換多少次預設全部替換,返回替換後的字串,也可以指定第二個返回值為替換的次數。
例子

local s,res,res1,res2
s = 'http://www.armfly.com'

--結果:http://test.armfly.com,1
res,count = string.gsub(s,'www','test')

--捕獲替換
--結果:test.freecls.abc
res = string.gsub(s,'^http://%w+.(%w+).com$','test.%1.abc')

--w替換成t,但是隻替換2次
--結果:http://ttw.armfly.com
res = string.gsub(s,'w','t',2)


string.format (formatstring, ···)
字串格式化型別c語言的sprintf不說廢話以例子來講解

local s = string.format('%d%s',123,'armfly')   --123armlfy

s = string.format('%0.2f',1.234343)     --1.23(保留2位)

--轉成16進位制,%X為大寫的16進位制
local s = string.format('%X',140)       --8C
local s = string.format('%x',140)       --8c
local s = string.format('%04x',140)     --008c


string.len(s)
返回字串長度=#s

string.rep(s,n)
字串重複n次並拼接返回

string.lower(s)
轉小寫

string.upper(s)
轉大寫

string.reverse(s)
反轉字串

微信公眾號:armfly_com 安富萊論壇:www.armbbs.cn 安富萊淘寶:https://armfly.taobao.com