lua-users home
lua-l archive

Re: split method?

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, 2011年06月27日 at 12:37 -0400, Dave Collins wrote:
> I’ve used this split (string to table) method I found here:
> http://stackoverflow.com/questions/1426954/split-string-in-lua.
> 
> 
> 
> I’ve got it working, but I don’t understand it.
Read the manual again, especially the part about Function calls (2.5.8).
> 
> 
> 
> function string:split(delimiter)
> 
> local result = { } 
> 
> local from = 1 
> 
> local delim_from, delim_to = string.find( self, delimiter, from
> ) 
> 
> while delim_from do 
> 
> table.insert( result, string.sub( self, from ,
> delim_from-1 ) ) 
> 
> from = delim_to + 1 
> 
> delim_from, delim_to = string.find( self, delimiter, from
> ) 
> 
> end 
> 
> table.insert( result, string.sub( self, from ) ) 
> 
> return result 
> 
> end 
> 
> 
> 
> This is how I’m calling it:
> 
> 
> 
> local myDateString = "2011年06月21日"
> 
> local myDateTbl = myDateString.split(myDateString,"-")
You could (should?) call it like that:
local myDateTbl = myDateString:split("-")
Function definitions using : add an implicit first parameter called
"self". Again, read section 2.5.8 of the manual (or the relevant
sections from the wonderful Lua Wiki).
</nk>

AltStyle によって変換されたページ (->オリジナル) /