实现lua 字符串spllit功能

string.split=function(str,token)
	ret={}
	_,token_num=string.gsub(str,token,'')
	f=''
	for i=1,token_num do
		f=f..'(.-)'..token
	end
	f=f..'(.+)'
	for j=1,token_num+1 do
        ret[j]=select(j+2,string.find(str,f))
	end
	return ret
end


test="5125,123,15,354"
o=string.split(test,',')
for k,v in pairs(o) do
	print("key:"..k..",  value:"..v)
end


写完之后,看到一个大神写的更加简单

string.split = function(s, p)
    local rt= {}
    string.gsub(s, '[^'..p..']+', function(w) table.insert(rt, w) end )
    return rt
end


使用例子一

local str = 'abc,123,hello,ok'

local list = string.split(str, ',')

for _, s in ipairs(list) do

    print(s)

end

 

结果:

abc

123

hello

ok

他的这种方法利用了gsub的一种参数方法

string.gsub (s, pattern, repl [, n])

Returns a copy of s in which all occurrences of the pattern 
have been replaced by a replacement string specified by repl,
which may be a string, a table, or a function. 
gsub also returns, as its second value, the total number of substitutions made.

repl是字符串,则为替换。 如果在参数前有%则表示符合匹配的字符串
If repl is a string, then its value is used for replacement. 
The character % works as an escape character:
any sequence in repl of the form %n, with n between 1 and 9, stands for the 
value of the n-th captured substring (see below). 
The sequence %0 stands for the whole match. The sequence %% stands for a single %.

repl作为表参数
If repl is a table, then the table is queried for every match, 
using the first capture as the key; if the pattern specifies 
no captures, then the whole match is used as the key.


如果参数为函数,则每次匹配成功则调用该函数
If repl is a function, then this function is called every 
time a match occurs, with all captured substrings passed 
as arguments, in order; 

if the pattern specifies no captures,
then the whole match is passed as a sole argument.

If the value returned by the table query or by the function call is a string or a number, 
then it is used as the replacement string; otherwise, if it is false or nil, 
then there is no replacement (that is, the original match is kept in the string).

参数n则限制最大
The optional last parameter n limits the maximum number of substitutions to occur.


举例:
   %1 表示符合模式的第一个匹配
   x = string.gsub("hello world", "(%w+)", "%1 %1")
   --> x="hello hello world world"
     
   第4项
   x = string.gsub("hello world", "%w+", "%0 %0", 1)
   --> x="hello hello world"
   
   hello 和from作为模式中左匹配为%1,world 和lua为右匹配,为%2
   x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")
   --> x="world hello Lua from"

   替换 以$打头的字符串
   x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)
   --> x="home = /home/roberto, user = roberto"
   
   参数为函数类型
   x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)
           return loadstring(s)()
         end)
     --> x="4+5 = 9"
     
    参数为表类型
   local t = {name="lua", version="5.1"}
   x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)
   --> x="lua-5.1.tar.gz"


版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-01-19 16:41  mooreliu  阅读(243)  评论(0编辑  收藏  举报