[REBOL] Re: Newbie Q. wrt Strings
From: gjones05:mail:orion at: 18-May-2001 7:14
From: "Patel, Sanjay"
> My query is if I have a string say
>
> data: "this is a test no 2"
>
> how do I manipulate the string so that
> I am left with the number 2. Or pull
> out the number 2 into another string/integer?
>
> I have tried using arrays but with no results (Arrays/Blocks).
>
> Any help would be appreciated!!
Hi, Sanjay,
Welcome, to REBOL. There are several ways to slice and dice this
tomato. I just throw a few out in order to give you some ideas:
1)
data: "this is a test no 2"
d-blk: parse data none ; yields ["this" "is" "a" "test" "no" "2"]
foreach d d-blk [print d]
; yields
;this
;is
;a
;test
;no
;2
print d-blk/6 ; yields "2"
type? d-blk/6 ; yields string
my-integer: to-integer d-blk/6
;yields integer type 2 through path notation
my-integer: to-integer pick d-blk 6
;yields integer type 2 through pick notation
2)
data: "this is a test no 2"
; now parse as a string
;move through "no" then through
;space then copy next info to my-data
;until reach following space then parse to
;end for parse result of "true" if needed
parse/all data [thru "no" thru " " copy my-data to " " to end]
print my-data ; yields "2"
my-int-data: to-integer my-data ; yields integer type 2
Of course, there are other ways ...
If you need a different approach, please feel free to respond.
--Scott Jones