首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

Lua实例

  • 25-04-20 18:20
  • 2601
  • 13623
juejin.cn

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}}"} ​
注:本文转载自juejin.cn的坤岭的文章"https://juejin.cn/post/7494546904757747746"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

142
代码人生
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top