How to split a string into a specific character in Rebol

I don't know how to split a string in a cleaner way.

 ref: copy/part (find line "#") -15
 rest2: copy/part (skip (find line "#") 1) 450

-15 to start and 450 to go to the end.

This is not nice, because I put the value.

What is the right solution?

+5
source share
2 answers

A warning ahead: there are many different ways to achieve this in Rebola. Therefore, you will probably receive several different offers.

To get started, let's stick with your initial approach to using FIND.

When you use FIND with a series, what you get is a new view of the base series data, located at a different offset from the start of the series data.

Let's start with the example data:

>> line: copy "this is foo#and here comes a long bar"
== "this is foo#and here comes a long bar"

Let FIND character #in this line and refer to the result as POS:

>> pos: find line "#"
== "#and here comes a long bar"

, (, REST2) . ( , LINE):

>> rest: copy next pos
== "and here comes a long bar"

COPY/part. "/part" (try help copy) : " " ( ). , POS. :

>> ref: copy/part line pos
== "this is foo"

! :

pos: find line "#"
ref: copy/part line pos
rest: copy next pos

, PARSE:

parse line [copy ref to "#" skip copy rest to end]

. PARSE, "Parsing" REBOL/Core ( REBOL 2.3, - REBOL 2 3).

: "#" , #"#" Rebol.

+5

set [ref rest2] parse line "#"  

, .

set [ref rest2] parse/all line "#"  

parse without/all Rebol csv Rebol.
/ "#" ..

== ["this" "" "foo" "" "" "" "" "" ]

, ref rest2

+2

All Articles