Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Newbie Q. wrt Strings

From: joel:neely:fedex at: 18-May-2001 7:47

Hi, Sanjay, There are a couple of ways I can think of immediately. Patel, Sanjay wrote:
> 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... >
Here's the demo
>> do %firstint.r >> data: "this is a test no 2"
== "this is a test no 2"
>> nodata: "there's no integer here"
== "there's no integer here"
>> find-first-int data
== 2
>> find-first-int nodata
== none
>> find-an-int data
== 2
>> find-an-int nodata
== none and here's the code 8<------------------------------------------------------------ find-first-int: func [ s [string!] /local digits nondigits partial ][ digits: charset [#"0" - #"9"] nondigits: complement digits partial: none either parse/all s [ any non-digits copy partial some digits to end ][ to-integer partial ][ none ] ] find-an-int: func [ s [string!] ][ foreach item to-block s [ if integer? item [ return item ] ] none ] 8<------------------------------------------------------------ The first version, FIND-FIRST-INT, explicitly parses the data string for a sequence of digit characters after skipping the leading non-digit characters. It's more work, but it gives a hint as to how you might handle more complex "find-the-data" tasks. The second version, FIND-AN-INT, lets REBOL do the heavy work of figuring out what's in the string. It's less work, but it depends on the fact that you're looking for something that REBOL has a pre-conceived notion about. Hope this helps! -jn- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com