lua字符串利用分隔符拆分

2023-12-29 12:41:46

把一个字符串用某个特定的符号去拆分它

----

--用symbol分隔字符串str,返回分隔后字符串数组

--@param str string 要分隔的字符串

--@param symbol string 分隔符

--@return table 分隔后的字符串数组

function splitSymbol(str, symbol)

????????local findStartIndex = 1

????????local splitIndex = 1

????????local splitedTable = {}

????????while true do

????????????????local nFindLastIndex = string.find(str, symbol, findStartIndex)

????????????????if not nFindLastIndex then

????????????????????????splitedTable[splitIndex] = string.sub(str, findStartIndex, string.len(str))

????????????????????????break

????????????????end

????????????????splitedTable[splitIndex] = string.sub(str, findStartIndex, nFindLastIndex - 1)

????????????????findStartIndex = nFindLastIndex + string.len(symbol)

????????????????splitIndex = splitIndex + 1

????????end

????????return splitedTable

end

文章来源:https://blog.csdn.net/win_huanglian/article/details/135263098
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。