返回顶部

Lua

Lua是一种面向过程的编程语言, Lua的值类型只有nil boolean number string。 Lua的引用类型只有:function table userdata thread。
注释使用 '--'

--字符串左填充
   function toright(str, len, filledChar)
    local function toright(str, len, filledChar)
      str    = tostring(str);
      filledChar  = filledChar or " ";
      local nRestLen  = len - #str;        -- 剩余长度
      local nNeedCharNum = nRestLen; -- 需要的填充字符的数量
      str = string.rep(filledChar, nNeedCharNum).. str;     -- 补齐
      return str;
    end
    if type(str)=="number" or type(str)=="string" then
        if not string.find(tostring(str),"\n") then
          return toright(str,len,filledChar)
        else
          str=string.split(str,"\n")
        end
    end
    if type(str)=="table" then
      local tmpStr=toright(str[1],len,filledChar)
      for i=2,#str do
        tmpStr=tmpStr.."\n"..toright(str[i],len,filledChar)
      end
      return tmpStr
    end
  end
--获取系统日期时间
os.date(“%Y-%m-%d %H:%M:%S”);
--字符串分割
function split(input,delimiter)
    input = tostring(input)
    delimiter = tostring(delimiter)
    if (delimiter == "") then return false end
    local pos, arr = 0, {}
    for st, sp in function() return string.find(input, delimiter, pos, true) end do
        table.insert(arr, string.sub(input, pos, st - 1))
        pos = sp + 1
    end
    table.insert(arr, string.sub(input, pos))
    return arr
end
--字符串 trim操作
token = token:gsub("^%s*(.-)%s*$", "%1");
--lua 枚举的使用:
--定义枚举:
function creatEnumTable(tbl) 
    local enumtbl = {} 
    local enumindex = 0 
    for i, v in ipairs(tbl) do 
        enumtbl[v] = enumindex + i 
    end 
    return enumtbl 
end 

--使用枚举:
--定义项:index      1         2	
trans_status = {"SUCCESS","FAILED"}
TRANS_STATUS = creatEnumTable(trans_status)
--访问枚举,得到值为 1
print(TRANS_STATUS.SUCCESS)
posted @ 2020-11-27 00:05  ToolsMan  阅读(102)  评论(0编辑  收藏  举报