[REBOL] Re: Splitting string based on substring separator
From: al:bri:xtra at: 22-Dec-2002 13:35
Frantisek Fuka wrote:
> Let's say I've got this string
>
> "HELLOsepP E O P L EsepHOWsepAREsepYOU"
>
> and I want to split it to substrings based on the separator string (in
this case, "sep"). So I want this result:
> ["HELLO" "P E O P L E" "HOW" "ARE" "YOU"]
>
> I know I can do this using parse and build the resulting block
programatically, but isn't there a simpler and cleaner solution? For
example, if the separator was just one character long, I know I'd just
> write:
>
> result: parse/all string-to-parse separator-character
How about this:
>> string-to-parse: "HELLOsepP E O P L EsepHOWsepAREsepYOU"
== "HELLOsepP E O P L EsepHOWsepAREsepYOU"
>> replace/all string-to-parse "sep" #"\"
== "HELLO\P E O P L E\HOW\ARE\YOU"
>> string-to-parse
== "HELLO\P E O P L E\HOW\ARE\YOU"
>> parse/all string-to-parse "\"
== ["HELLO" "P E O P L E" "HOW" "ARE" "YOU"]
Of couse this requires using a separator character that isn't used in the
original string...
I hope that helps!
Andrew Martin
ICQ: 26227169 http://valley.150m.com/