[REBOL] Re: Splitting string based on substring separator
From: greggirwin:mindspring at: 22-Dec-2002 0:37
Hi Frantisek,
FF> I know I can do this using parse and build the resulting block
FF> programatically, but isn't there a simpler and cleaner solution? For
FF> example, if the separator was just one character long, I know I'd just
FF> write:
Well, I shouldn't respond late at night...and I think I should have
one of those in my toolbox already, but I can't find it right now, so
here's what I came up with which, while not terribly clean, isn't so
bad if you only have to write it once.
split-dlm-str: func [string dlm /local action result] [
result: copy []
action: (to paren! [append result data])
parse string compose/deep [
some [copy data to (dlm) (action) (length? dlm) skip]
copy data to end (action)
]
result
]
>> s: "HELLOsepP E O P L EsepHOWsepAREsepYOU"
== "HELLOsepP E O P L EsepHOWsepAREsepYOU"
>> dlm: "sep"
== "sep"
>> split-dlm-str s dlm
== ["HELLO" "P E O P L E" "HOW" "ARE" "YOU"]
>> s: "sepHELLOsepP E O P L EsepHOWsepAREsepYOUsep"
== "sepHELLOsepP E O P L EsepHOWsepAREsepYOUsep"
>> dlm: "sep"
== "sep"
>> split-dlm-str s dlm
== [none "HELLO" "P E O P L E" "HOW" "ARE" "YOU" none]
Maybe someone else will jump in with a better solution. Like I said,
it's late.
-- Gregg