Lua 實現字串的分割

2021-09-13 03:06:12 字數 571 閱讀 4165

在開發過程中,經常會用到字串分割,如c#中的string.split函式,把乙個字串按某個子字串分割成乙個字串陣列。

基本思想:

使用lua自帶的string.gmatch()函式實現,此函式可以對乙個字串做正規表示式匹配,返回乙個迭代器。

-- 分割字串

---@param str string 元字串

---@param seq string 分割字元

---@return table

function common.stringsplit(str,seq)

if str == nil or type(str) ~= "string" then

return nil

endif pattern == nil then

return str

endlocal r = {}

for word in string.gmatch(str,"[^*]+") do

table.insert(r,word)

endreturn r

end

lua 分割字串

function brjss.stringtotable s local tb local len 0 if nil s or string.len s 1 then return tb,len end utf8的編碼規則 1.字元的第乙個位元組範圍 0x00 0x7f 0 127 或者 0xc2 ...

Lua的字串分割函式

lua中的字串函式 函式功能 string.len s 返回字串s的長度 string.rep s,n 返回重複n次字串s的串 你使用string.rep a 2 20 可以建立乙個1m bytes的字串 string.lower s 將s中的大寫字母轉換成小寫 string.upper s 將s中...

LUA裡按 分割字串

for mu id in string.gmatch mu info,d do print mu id mu id end.任意字元 a 字母 c 控制字元 d 數字 l 小寫字母 p 標點字元 s 空白符 u 大寫字母 w 字母和數字 x 十六進製制數字 z 代表 0的字元 特殊字元如下 也作為以...