string.find (s, pattern [, init [, plain]])
s 原字符串
pattern 需要匹配的字符串
init 正数表示从第几位开始匹配 负数则是从倒数第几位开始匹配
plain 默认为false 是否模式匹配,如果是普通的查找最好置为true
参数为true函数只默认简单查找子串"%d"。
参数为false时,函数会按模式匹配查找,"%d"表示查找一个数字。
local curTimeZoneSec = os.difftime(os.time(), os.time(os.date("!*t", os.time())))
curTimeZoneSec就是距离格林威治时间的偏移量,计算出小时数和分钟数就是本地的时区的显示格式了
os.date('!*t',os.time()) 获取的为格林威治时间
----------------------BASE 64 coding-------------------------
-- character table string
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
-- encoding
function string.base64Encoding(data)
return ((data:gsub('.', function(x)
local r,b='',x:byte()
for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
return r;
end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if (#x < 6) then return '' end
local c=0
for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
return b:sub(c+1,c+1)
end)..({ '', '==', '=' })[#data%3+1])
end
-- decoding
function string.base64Decoding(data)
data = string.gsub(data, '[^'..b..'=]', '')
return (data:gsub('.', function(x)
if (x == '=') then return '' end
local r,f='',(b:find(x)-1)
for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
return r;
end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
if (#x ~= 8) then return '' end
local c=0
for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(8-i) or 0) end
return string.char(c)
end))
end
-11 % 100 --89
并不是-11
math.modf(1.12) --1,0.12
math.modf(2) -- 2,0
math.modf(-1.23) -- -1,-0.23
math.floor(num + 0.5) --加个0.5就好
local c = string.sub(str,i,i)
print(os.time())
print(os.time({year=1970,month=1,day=1,hour=8,min=0,sec=0})) --0
print(os.date("%Y-%m-%d %H:%M:%S",os.time())) -- 2019-06-08 17:56:54
dump(os.date("*t"),"格式化") --返回一个table
function rconcat(l)
if type(l) ~= "table" then return l end
local res = {}
for i=1,#l do
res[i] = rconcat(l[i])
end
return table.concat(res,' ')
end
print(rconcat( {{'i','am'},{{'boy'},{','},{'you','are'},'girl'}}))
深度访问叠加层次的table的元素来链接
concat第二个可选参数,指定间隔字符串,第三第四个为 开始位置,结束位置
t = {}
t['uu'] = 20;
t['aa'] = 55;
t['sss'] = 100;
t['tt'] = 888;
function iterr(t,f)
local a = {}
for n in pairs(t) do a[#a+1] = n end
table.sort(a,f)
local i = 0
return function()
i = i + 1
return a[i],t[a[i]]
end
end
function pairsByKays(t,f)
return iterr(t,f),t,nil
end
com = function(a,b) --降序,重写<的比较方法
if a > b then return true
elseif a < b then return false
end
return false --相等时要返回false
end
for k,v in pairsByKays(t,com) do
print(k,v)
end
因为table的sort只是对值进行排序的,默认是升序排序,可以使用一个方法重写<去改成降序排序,注意相等的情况要返回false
要对table的key值排序就需要做一个辅助的迭代器,将key复制到一个新table中进行排序,然后返回原来的数据就好了。
sort方法,只对key值是连续的table有效,1,2,3...的那种