Lua——库函数

-----------------------------------------库函数
--*********************************************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)
end
print("*******************************")
--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
    end
end
print(#t)
for k,v in ipairs(t) do
    print(k,v)
end
end
function SetInsertValues(table)
    for k,v in ipairs(table) do
        print(k,v)
    end
end
t={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(),传入要寻找的字符串,若找到,则返回该字符在字符串中的开始和结束的位置;若没有找到,则返回nil
print("通过位置,重新打印出字符:",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] = w
end
for k,v in ipairs(words) do
    nowWords = nowWords .." " ..v
    print(nowWords) --打印结果
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值