Manipulating String values?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Manipulating String values?

ABDEEN
Helo,
With string values, I don't know if I can copy, or find a string partition value!
For example, how can I get from 'Hello anyone' the string 'Hello'? I would like find the first index of the partition ' anyone'??
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating String values?

Travis Griggs-3
On Jan 18, 2007, at 8:38, ABDEEN wrote:

Helo,
With string values, I don't know if I can copy, or find a string partition value!
For example, how can I get from 'Hello anyone' the string 'Hello'? I would like find the first index of the partition ' anyone'??

You can search for the indexes of substrings and copy subsets with something like:

string := 'Hello anyone'.
startOfAnyone := string indexOfSubCollection: 'anyone' startingAt: 1.
string copyFrom: 1 to: startOfAnyone - 1.

You could also do it via "streaming" operations:
stream := string readStream.
stream upToAll: 'anyone'

Or you could tokenize it as words and then simply grab the word before the anyone token.
words := string runsFailing: [:char | char isSeparator].
words before: 'anyone'

HTH

--
Travis Griggs
Objologist
"Every institution finally perishes by an excess of its own first principle." - Lord Acton



jas
Reply | Threaded
Open this post in threaded view
|

Re: Manipulating String values?

jas
At 08:58 AM 1/18/2007, Travis Griggs wrote:
On Jan 18, 2007, at 8:38, ABDEEN wrote:

Helo,
With string values, I don't know if I can copy, or find a string partition value!
For example, how can I get from 'Hello anyone' the string 'Hello'? I would like find the first index of the partition ' anyone'??

You can search for the indexes of substrings and copy subsets with something like:

string := 'Hello anyone'.
startOfAnyone := string indexOfSubCollection: 'anyone' startingAt: 1.
string copyFrom: 1 to: startOfAnyone - 1.


Flunk.


You could also do it via "streaming" operations:
stream := string readStream.
stream upToAll: 'anyone'


Flunk.


Or you could tokenize it as words and then simply grab the word before the anyone token.
words := string runsFailing: [:char | char isSeparator].
words before: 'anyone'


Interesting!  But still off the mark, so - Flunk.
( Partial result loses data in both halves of the partition.
  Not now, of course - works fine on the test data.
  Oh, if only we could write statically provably type safe code,
  our programs would be far less, um, er, what - readable?
  Who knows - maybe your interpretation was fine.
  Or maybe my insanely pedantic reading was the real intent.
  A fabulous example of where the wild things, actually, are.
).


So -- given:

    string := 'Hello anyone'

One might try something like

    partitionIndex := string indexOfSubcollection: 'anyone' startingAt: 1.
    hello := string copyFrom: 1 to: partitionIndex - 2.

or

    stream := string readStream.
    hello := stream upToAll: ' anyone'


Or even

    stream := string readStream.
    find := 'anyone' readStream.
    ws := WriteStream on: String new.
    [ stream atEnd
      or: [ [ find peekFor: stream peek
            ] whileFalse:
                  [ws nextPut: stream next].
            [ (find peekFor: stream next)
              and: [stream atEnd not]
            ] whileTrue.
            find atEnd
          ]       
    ] whileFalse:
          [ws nextPutAll: find contents.
           find reset
          ].
    find atEnd
        ifTrue: [hello := ws contents]

if it just HAS TO scale,
and you or it be smokin'...


Cheers,

-cstb


Reply | Threaded
Open this post in threaded view
|

Re: Manipulating String values?

Dave Stevenson-2
In reply to this post by ABDEEN
This also might interest you:
        'Hello anyone' tokensBasedOn: Character space

Dave

ABDEEN wrote:
> Helo,
> With string values, I don't know if I can copy, or find a string
> partition value!
> For example, how can I get from 'Hello anyone' the string 'Hello'? I
> would like find the first index of the partition ' anyone'??

Reply | Threaded
Open this post in threaded view
|

Re: Manipulating String values?

Travis Griggs-3
In reply to this post by jas
On Jan 18, 2007, at 18:34, cstb wrote:

> At 08:58 AM 1/18/2007, Travis Griggs wrote:
>> On Jan 18, 2007, at 8:38, ABDEEN wrote:
>>
>>> Helo,
>>> With string values, I don't know if I can copy, or find a string  
>>> partition value!
>>> For example, how can I get from 'Hello anyone' the string  
>>> 'Hello'? I would like find the first index of the partition '  
>>> anyone'??
>>
>> You can search for the indexes of substrings and copy subsets with  
>> something like:
>>
>> string := 'Hello anyone'.
>> startOfAnyone := string indexOfSubCollection: 'anyone' startingAt: 1.
>> string copyFrom: 1 to: startOfAnyone - 1.
>
>
> Flunk.
>
>
>> You could also do it via "streaming" operations:
>> stream := string readStream.
>> stream upToAll: 'anyone'
>
>
> Flunk.
>
>
>> Or you could tokenize it as words and then simply grab the word  
>> before the anyone token.
>> words := string runsFailing: [:char | char isSeparator].
>> words before: 'anyone'
>
>
> Interesting!  But still off the mark, so - Flunk.
> ( Partial result loses data in both halves of the partition.
>   Not now, of course - works fine on the test data.
>   Oh, if only we could write statically provably type safe code,
>   our programs would be far less, um, er, what - readable?
>   Who knows - maybe your interpretation was fine.
>   Or maybe my insanely pedantic reading was the real intent.
>   A fabulous example of where the wild things, actually, are.
> ).
>
>
> So -- given:
>
>     string := 'Hello anyone'
>
> One might try something like
>
>     partitionIndex := string indexOfSubcollection: 'anyone'  
> startingAt: 1.
>     hello := string copyFrom: 1 to: partitionIndex - 2.
>
> or
>
>     stream := string readStream.
>     hello := stream upToAll: ' anyone'
>
> .
> Or even
>
>     stream := string readStream.
>     find := 'anyone' readStream.
>     ws := WriteStream on: String new.
>     [ stream atEnd
>       or: [ [ find peekFor: stream peek
>             ] whileFalse:
>                   [ws nextPut: stream next].
>             [ (find peekFor: stream next)
>               and: [stream atEnd not]
>             ] whileTrue.
>             find atEnd
>           ]
>     ] whileFalse:
>           [ws nextPutAll: find contents.
>            find reset
>           ].
>     find atEnd
>         ifTrue: [hello := ws contents]
>
> if it just HAS TO scale,
> and you or it be smokin'...

ROTFL

"You are a sad strange little man. You have my pity."

--
Buzz Lightyear
Space Ranger