Lua实例
字符串
字符串截取
python 代码解读复制代码local str="hello world!" string.sub(str,1,string.len(str)-1) -- hello word print(string.sub(str,1,string.len(str)-1) )
查找字符串
在一个指定的目标字符串中搜索指定的内容(第三个参数为索引),返回其具体位置。不存在则返回 nil。
lua 代码解读复制代码string = "Lua Tutorial" print(string.find(string,"bb")) nil
string.find("Hello Lua user", "Lua", 1) 7 9
计算字符串长度。
lua 代码解读复制代码string.len(arg) string.len("abc") 3
链接两个字符串
shell 代码解读复制代码 > print("www.runoob.".."com") www.runoob.com
数字
Lua取整数部分
lua 代码解读复制代码math.ceil()
math.ceil(12.2)//返回13
math.ceil(12.7)//返回13
math.ceil(12.0)// 返回12
转换成字符串
vbscript 代码解读复制代码tostring()可以将布尔类型和数值类型转换为字符串类型,示例:
local num1 = 10;
local num2 = 10.0;
local num3 = 10.03;
print(tostring(num1)); --输出"10"
print(tostring(num2)); --输出"10"
print(tostring(num3)); --输出"10.03"
转换成数字
ini 代码解读复制代码local num = tonumber("10"); -- 返回十进制数10
local num = tonumber("AF",16); -- 返回十六进制数175
local num = tonumber("0xA"); -- 返回10
local num = tonumber("56.9"); -- 返回56.9
local num = tonumber("0102"); -- 返回102
local num = tonumber("123456red"); -- 返回nil
local num = tonumber("red"); -- 返回nil
local num = tonumber("true"); -- 返回nil
local num = tonumber({x =10, y = 20});-- 返回nil
正则匹配
python 代码解读复制代码str = "b00011"
print(string.match(str, "%d+"))
print(string.match(str, "%a+"))
运行结果
00011
b
string.match(s, pattern[, init])
在字符串s中匹配pattern,如果匹配失败返回nil
%d 匹配数字0-9
%a 匹配字母,无论大小写
+ 匹配前一字符1次或多次
循环
java 代码解读复制代码for i = 0, 10, 1 do
print("continue code here")
end
给整数数字前面补零
lua 代码解读复制代码-- 计算数字的位数
function DightNum(num)
if math.floor(num) ~= num or num < 0 then
return -1
elseif 0 == num then
return 1
else
local tmp_dight = 0
while num > 0 do
num = math.floor(num/10)
tmp_dight = tmp_dight + 1
end
return tmp_dight
end
end
-- 在整数数字前面加0,例:3 变成 "03"
function AddZeroFrontNum(dest_dight, num)
local num_dight = DightNum(num)
if -1 == num_dight then
return -1
elseif num_dight >= dest_dight then
return tostring(num)
else
local str_e = ""
for var =1, dest_dight - num_dight do
str_e = str_e .. "0"
end
return str_e .. tostring(num)
end
end
local a=3
local s=AddZeroFrontNum(5,a)
print(s)
批量增加渠道
lua 代码解读复制代码-- 字符串分割
function Split(szFullString, szSeparator)
local nFindStartIndex = 1
local nSplitIndex = 1
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - 1)
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex + 1
end
return nSplitArray
end
-- 计算数字的位数
function DightNum(num)
if math.floor(num) ~= num or num < 0 then
return -1
elseif 0 == num then
return 1
else
local tmp_dight = 0
while num > 0 do
num = math.floor(num/10)
tmp_dight = tmp_dight + 1
end
return tmp_dight
end
end
-- 在整数数字前面加0,例:3 变成 "03"
function AddZeroFrontNum(dest_dight, num)
local num_dight = DightNum(num)
if -1 == num_dight then
return -1
elseif num_dight >= dest_dight then
return tostring(num)
else
local str_e = ""
for var =1, dest_dight - num_dight do
str_e = str_e .. "0"
end
return str_e .. tostring(num)
end
end
--渠道包测试
sqlInfo["stat_reg_src_add"] = {database="btt",param = {"proj","db","sel_app","media","agency_croe","agency_opti","account","src","account_id"},setParam = function(input,myself)
input.sql_condition = ""
local src =input.src
local find_src = string.find(src,",")
if find_src == nil then
input.sql_condition =input.sql_condition .. "('" .. input.src .. "','".. input.account .."','".. input.agency_croe .."','" .. input.agency_opti .."','" .. input.media .."','" .. input.sel_app .."','" .. input.account_id .."')"
else
local list = Split(src, ",")
if list[3] == nil then
local list_st =list[1]
local list_en =list[2]
local list_st_a =string.match(list_st, "%a+")
local list_st_d =string.match(list_st, "%d+")
local list_en_a =string.match(list_en, "%a+")
local list_en_d =string.match(list_en, "%d+")
local list_st_d_length =string.len(list_st_d)
if list_st_a ==list_en_a then
for i = list_st_d, list_en_d, 1 do
local s=AddZeroFrontNum(list_st_d_length,math.ceil(i))
local ss =list_st_a..tostring(s)
if input.sql_condition ~= '' then
input.sql_condition =input.sql_condition .. ","
end
input.sql_condition =input.sql_condition .. "('" .. ss .. "','" .. input.account .."','".. input.agency_croe .."','" .. input.agency_opti .."','" .. input.media .."','" .. input.sel_app .."','" .. input.account_id .."')"
end
else
error("请确认格式前缀!")
end
else
error("格式不对!")
end
end
local param_key = {"sql_condition"}
for k,v in pairs(param_key) do
local isFind = false
for k1,v1 in pairs(myself.param) do
if v1 == v then
isFind = true
break
end
end
if not isFind then
table.insert(myself.param,v)
end
end
return input,myself
end,
sql = "insert into {{db}}.reg_src (src,account,agency_croe,agency_opti,media,sel_app,account_id) values {{sql_condition}}"}
评论记录:
回复评论: