[REBOL] Re: Splitting string based on substring separator
From: g:santilli:tiscalinet:it at: 22-Dec-2002 11:33
Hi Andreas,
On Sunday, December 22, 2002, 2:23:19 AM, you wrote:
AB> -- snip --
AB> split: func [ string delim /local tokens token pos ] [
AB> string: copy string ; resets series pointer - index? points to 1
AB> tokens: copy []
AB> while [ (not tail? string) and (found? pos: find string delim) ] [
AB> token: copy/part string -1 + index? pos
AB> string: copy skip string (length? token) + (length? delim)
AB> append tokens token
AB> ]
AB> append tokens copy string
AB> ]
AB> -- snap --
If I was to use FIND, I'd do it this way:
split: func [string delim /local tokens pos] [
tokens: make block! 32
while [pos: find string delim] [
append tokens copy/part string pos
string: skip pos length? delim
]
append tokens copy string
]
but I'd prefer a PARSE version anyway:
split: func [string delim /local tokens token] [
tokens: make block! 32
parse/all string [
any [copy token to delim (append tokens token) delim]
copy token to end (append tokens token)
]
tokens
]
Anyway, I agree with Frantisek Fuka that this should probably be
done natively in PARSE (the real problem is, finding the right
name for the refinement!)
Regards,
Gabriele.
--
Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer
Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r