Skip to content

常用函式

基礎函式

函式名函式功能示例
type(v)用來判斷v的型別, 返回字串"nil", "number", "string", "boolean", "table", "function", "thread", "userdata"
tonumber(e [,base])將字串轉換成數字,把e(必須為數字或者是可以轉成數字的字串)轉成10進位制數字,base為多少進位制(可以為2-36),預設為10tonumber('10')10
tostring(var)將變數轉換成字串,把任意型別的e已適當的方式轉成字串,如果e的原表有__tostring函式,則呼叫並傳入e作為引數,把返回值作為結果返回。math.asin(0.5)0.52359877
isboolean(var)檢查是否為boolean型別
isnil(var)檢查是否為nil
isstring(var)檢查是否為字串型別
isinteger(var)檢查是否為整數型別

數學函式

函式名函式功能示例示例結果
abs取絕對值math.abs(-15)15
acos反餘弦函式math.acos(0.5)1.04719755
asin反正弦函式math.asin(0.5)0.52359877
atan2x / y的反正切值math.atan2(90.0, 45.0)1.10714871
atan反正切函式math.atan(0.5)0.4636476095
ceil不小於x的最大整數math.ceil(5.8)6
cosh雙曲線餘弦函式math.cosh(0.5)1.276259652
cos餘弦函式math.cos(0.5)0.87758256
deg弧度轉角度math.deg(math.pi)180
exp計算以e為底x次方值math.exp(2)2.718281828
floor不大於x的最大整數math.floor(5.6)5
fmod (mod)取模運算math.mod(14, 5)4
frexp把雙精度數val分解為數字部分(尾數)和以2為底的指數n,即val=x*2nmath.frexp(10.0)0.625 4
ldexp計算value * 2的n次方math.ldexp(10.0, 3)80 = 10 * (2 ^3)
log10計算以10為基數的對數math.log10(100)2
log計算一個數字的自然對數math.log(2.71)0.9969
max取得引數中最大值math.max(2.71, 100, -98, 23)100
min取得引數中最小值math.min(2.71, 100, -98, 23)-98
modf把數分為整數和小數math.modf(15.98)15 98
pow把數分為整數和小數math.pow(2, 5)32
rad角度轉弧度math.rad(180)3.141592654
random獲取隨機數math.random(1, 100)在使用math.random函式之前必須使用此函式設定隨機數種子
randomseed設定隨機數種子math.randomseed(os.time())0.5210953
sinh雙曲線正弦函式math.sinh(0.5)0.5
sin正弦函式math.sin(math.rad(30))15
sqrt開平方函式math.sqrt(16)4
tanh雙曲線正切函式math.tanh(0.5)0.46211715
tan正切函式math.tan(0.5)0.5463024

字串函式

序號方法 & 用途
1string.upper(argument):
字串全部轉為大寫字母。
2string.lower(argument):
字串全部轉為小寫字母。
3string.sub(s, i [, j]):
s 為要擷取的字串,i 為擷取開始位置,j 為擷取結束位置,預設為 -1,最後一個字元。
4string.gsub(mainString,findString,replaceString,num)
在字串中替換。
mainString 為要操作的字串, findString 為被替換的字元,replaceString 要替換的字元,num 替換次數(可以忽略,則全部替換),如:
string.gsub("aaaa","a","z",3);
zzza    3
5string.find (str, substr, [init, [plain]])
在一個指定的目標字串 str 中搜索指定的內容 substr,如果找到了一個匹配的子串,就會返回這個子串的起始索引和結束索引,不存在則返回 nil。
init 指定了搜尋的起始位置,預設為 1,可以一個負數,表示從後往前數的字元個數。
plain 表示是否使用簡單模式,預設為 false,true 只做簡單的查詢子串的操作,false 表示使用使用正則模式匹配。
以下例項查詢字串 "Lua" 的起始索引和結束索引位置:
string.find("Hello Lua user", "Lua", 1)
7 9
6string.reverse(arg)
字串反轉
string.reverse("Lua")
auL
7string.format(...)
返回一個類似printf的格式化字串
string.format("the value is:%d",4)
the value is:4
8string.char(arg) 和 string.byte(arg[,int])
char 將整型數字轉成字元並連線, byte 轉換字元為整數值(可以指定某個字元,預設第一個字元)
string.char(97,98,99,100)
abcd
string.byte("ABCD",4)
68
string.byte("ABCD")
65
9string.len(arg)
計算字串長度
string.len("abc")
3
10string.rep(string, n)
返回字串string的n個複製
string.rep("abcd",2)
abcdabcd
11..
連結兩個字串
print("www.ai-v".."ision.com")
www.ai-vision.com
12string.gmatch(str, pattern)
返回一個迭代器函式,每一次呼叫這個函式,返回一個在字串 str 找到的下一個符合 pattern 描述的子串。如果引數 pattern 描述的字串沒有找到,迭代函式返回nil
for word in string.gmatch("Hello ai vision", "%a+") do print(word) end
Hello
ai
vision
13string.match(str, pattern, init)
string.match()只尋找源字串str中的第一個配對. 引數init可選, 指定搜尋過程的起點, 預設為1。
在成功配對時, 函式將返回配對錶達式中的所有捕獲結果; 如果沒有設定捕獲標記, 則返回整個配對字串. 當沒有成功的配對時, 返回nil
string.match("I have 2 questions for you.", "%d+ %a+")
2 questions
string.format("%d, %q", string.match("I have 2 questions for you.", "(%d+) (%a+)"))
2, "questions"

Vision Studio