記錄下工作中用到lua的乙個簡單功能需求:
如下字串:"aa,bb;cc,dd;ee,ff;gg,hh;"
轉成:表形式。
function combination(sourcestr)
if not sourcestr or sourcestr == "" then
print("特定字串轉成指定表出錯!")
return
endlocal outsplitchar = ";"
local innersplit = ","
local function inner_get_table_from_split_string(src, splitchar)
local splitlist = {}
string.gsub(src, '[^' .. splitchar ..']+', function(value) table.insert(splitlist, value) end)
return splitlist
endlocal out_tbl = inner_get_table_from_split_string(sourcestr, outsplitchar)
local tmp={}
for _, value in pairs(out_tbl) do
local tmp_tbl = inner_get_table_from_split_string(value, innersplit)
local inner_tbl = {}
inner_tbl["" .. tmp_tbl[1]] = tonumber(tmp_tbl[2])
tmp[#tmp + 1] = inner_tbl
endlocal final_tbl = {}
for k, v in pairs(tmp) do
final_tbl[table.keys(v)[1]] = table.values(v)[1]
endreturn final_tbl
end
這裡的分號和逗號我直接寫在了方法裡,你也可以用引數的形式傳入,這樣就比較靈活,當然特定字串也不限於分號和逗號…… 擷取字串中特定字元
string.prototype.trim function function getcontent str index 0 var patt new regexp s var patt s while result patt.exec str null for var i 0 i index i ...
通過特定的字元擷取字串
方法 string address 上海市上海市浦東新區金橋鎮金葵新城2223號 string addressname 浦東新區 system.out.println address.indexof addressname system.err.println address.substring a...
擷取字串
static function blogsummary str,len 100 else out valtmp break tmp tmpstr outlen mb strlen valtmp,charset out val.rs 2 key right rs 2 key unset rs tags...