lua 函數

1. lua 函數 默認返回  nil  安全

2.  tonumber("")   返回 nil 、、 tonumber(nil)   返回  nil 、、 tonumber("wq") 返回 nil函數

3. string.format("the item you want to set %[c/d/s]",value)lua

%c - 接受一個數字, 並將其轉化爲ASCII碼錶中對應的字符spa

%d, %i - 接受一個數字並將其轉化爲有符號的整數格式
%o - 接受一個數字並將其轉化爲八進制數格式
%u - 接受一個數字並將其轉化爲無符號整數格式
%x - 接受一個數字並將其轉化爲十六進制數格式, 使用小寫字母
%X - 接受一個數字並將其轉化爲十六進制數格式, 使用大寫字母
%e - 接受一個數字並將其轉化爲科學記數法格式, 使用小寫字母e
%E - 接受一個數字並將其轉化爲科學記數法格式, 使用大寫字母E
%f - 接受一個數字並將其轉化爲浮點數格式
%g(%G) - 接受一個數字並將其轉化爲%e(%E, 對應%G)及%f中較短的一種格式
%q - 接受一個字符串並將其轉化爲可安全被Lua編譯器讀入的格式
%s - 接受一個字符串並按照給定的參數格式化該字符串code

 

4. 把字符串按分隔符分割的函數

local 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

 5. 把字符串按指定分隔符分割的函數

function split_by(str, separator)
    local ret = {}
    if str and separator and str ~= "" and separator ~= "" then
        string.gsub(str, "[^" .. separator .. "]+", function(w) table.insert(ret, w) end)
    end
    return ret
end
相關文章
相關標籤/搜索