Lua——库函数

来源:互联网 发布:徐沪生 知乎 黑历史 编辑:程序博客网 时间:2024/05/21 03:24
-----------------------------------------库函数--*********************************************math.randomseed()******--01. math.randomseed(),用于在同一时间相同随机范围的随机数的序列化。(例如下:有两个math.random(10),在两个随机钱添加math.randomseed(os.time()),则得到的--随机数是相同的。他们在同一时间运行,因此os.time()的值是相等的(得到的值为当前的运行时间)math.randomseed(os.time())for var=0,10 do    RandomNumber = math.random(10)    print(RandomNumber)endprint("*******************************")--math.randomseed(os.time())for var=0,10 do    RandomNumber = math.random(10)    print(RandomNumber)end--*********************************************table.insert()&table.remove()********local t={}function TableInsert()    print("请输入插入表格的值,若输入结束,请输入“break out”结束输入!")    for lines in io.lines("insertList") do    table.insert(t,lines)    if(lines == "break out") then        break    endendprint(#t)for k,v in ipairs(t) do    print(k,v)endendfunction SetInsertValues(table)    for k,v in ipairs(table) do        print(k,v)    endendt={11,111,233,666}table.insert(t,2,654)SetInsertValues(t)print("******************************")table.remove(t,2)SetInsertValues(t)--********************************************table.sort()排序print("******************************")table.sort(t)SetInsertValues(t)--********************************************连接function rConcat(params)    if type(params) ~= "table" then        return params       end    local res = {}    for i=1,#params do        res[i] = rConcat(params[i]) --递归,将表中的表遍历出来    end    print(table.concat(res))    return table.concat(res)end--print(rConcat{{"a ",{"nice "}},"and ",{{"log "},{"List"}}})-----------------------------------------------字符串库--**********************************基础字符串函数strings = "this is a string stence"print("字符串长度:",string.len(strings)) --获取字符串长度print("重复(n次)指定的字符串:",string.rep(strings,4))    --重复(n次)指定的字符串print("字符串转小写:",string.lower(strings))  --返回一个传入参数(字符串)的副本,并将所有字符转变为小写print("字符串转大写",string.upper(strings))   --与string.lower()相对,全部转为大写print("提取第i到j个字符:",string.sub(strings,1,7)) --提取字符串中第i到j位置的字符串--这里 -1表示字符串的末尾第一位,以此类推.string.sub()不会改变原字符串的值,只会返回一个新值。print("将数字转为对应的ASCII字符:",string.char(48))   --string.char()将数字转为对应的ASCII字符print("将字符串中第i个字符转为ASCII码:",string.byte(strings,3,4))   --string.byte(sting,i,j)将字符串第i个位置的字符转为ASCII码,第二个参数为可选项,j的默认值为i。print("格式化字符串:",string.format("pi=%.4f",math.pi))   --格式化输出,前面以%起,用不同的字母等设置输出格式,.4f为输出小数点后4位的浮点型。--**********************************模式匹配函数------****************************string.find()s="this is a function test!"i,j = string.find(s,"function")print("要寻找的字符串的始末位置:",i,j)  --通过string.find(),传入要寻找的字符串,若找到,则返回该字符在字符串中的开始和结束的位置;若没有找到,则返回nilprint("通过位置,重新打印出字符:",string.sub(s,i,j))    --验证:重新打印出该位置的字符print("固定模式中,找到的对应字符串:",string.match(s,"function")) --string.match(),与string.find()相似,但是并不是返回所寻字符串在对应字符串中的位置,而是直接返回该字符串date="Today is 7/3/2017"print("变量模式,打印结果:" ,string.match(date,"%d+%d+%d+")) --在变量模式中,通过此从字符中取到number串------****************************string.gsub()stringTest ="It's a wonderful day!It's wonderful!"s1=string.gsub(stringTest,"wonderful","terribel")   --string.gsub()在没有使用第四个参数进行限制时,将会把字符串中所有符合的字符全部替换为目标字符print("(未使用第四参数)替换后:",s1)   s2=string.gsub(stringTest,"wonderful","terribel",1) --在使用第四个参数,即替换的个数,限制后只会进行该个数进行替换print("(使用第四参数)替换后:",s2)count = select(2,string.gsub(stringTest,"wonderful","terribel"))print(count)------****************************string.gmatch()str ="It's a wonderful day!It's wonderful!"words = {}local nowWords = ""for w in string.gmatch(str,"%a+") do    -- “%a+”表示匹配一个或多个字母字符的序列(单词)    words[#words+1] = wendfor k,v in ipairs(words) do    nowWords = nowWords .." " ..v    print(nowWords) --打印结果end
原创粉丝点击