Best way to find if a string begins with a particular string..

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

Best way to find if a string begins with a particular string..

Rick Flower-2
Hi all..

So I've been writing code this morning to parse various things and I'm
not
sure what the best way to do for this next piece I need..

I need to determine if a string (read in from a stream) has a specified
string prefix present or not and return a boolean..

So, below are some example strings and the respective results :

'FooBarBaz' isStringPrefixPresent: 'Foo' -->true

'FooBarBaz' isStringPrefixPresent: 'Bar' --> false

'./mycoolfilename' isStringPrefixPresent: './' --> true

Is there a good way to do this without funny looping character by
character? I'll probably do that initially but thought I'd ask from
you that probably know better than I do..

Thx!

--Rick

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

Holger Freyther
On Tue, Oct 16, 2012 at 11:41:06AM -0700, Rick Flower wrote:
> Hi all..

Hi,

>
> Is there a good way to do this without funny looping character by
> character? I'll probably do that initially but thought I'd ask from
> you that probably know better than I do..

might still be funny looping but you could be using PetitParser (i
have a version of it on gitorious).

holger

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

Rick Flower-2
 

Thanks.. I'll check it out if nothing else pans out!
_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

MrGwen
In reply to this post by Rick Flower-2
On 16/10/2012 20:41, Rick Flower wrote:

> Hi all..
>
> So I've been writing code this morning to parse various things and I'm
> not
> sure what the best way to do for this next piece I need..
>
> I need to determine if a string (read in from a stream) has a specified
> string prefix present or not and return a boolean..
>
> So, below are some example strings and the respective results :
>
> 'FooBarBaz' isStringPrefixPresent: 'Foo' -->true
>
> 'FooBarBaz' isStringPrefixPresent: 'Bar' --> false
>
> './mycoolfilename' isStringPrefixPresent: './' --> true
>
> Is there a good way to do this without funny looping character by
> character? I'll probably do that initially but thought I'd ask from
> you that probably know better than I do..
>
> Thx!
>
> --Rick
>
> _______________________________________________
> help-smalltalk mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-smalltalk

Hi,

you can use startsWith:

cheers,
gwen

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

Paolo Bonzini-2
In reply to this post by Holger Freyther
Il 16/10/2012 23:18, Holger Hans Peter Freyther ha scritto:
>> > Is there a good way to do this without funny looping character by
>> > character? I'll probably do that initially but thought I'd ask from
>> > you that probably know better than I do..
> might still be funny looping but you could be using PetitParser (i
> have a version of it on gitorious).

That's slightly heavy artillery. :)

Gwen has just beaten me to the answer, the method you want is #startsWith:.

Remember to look for methods in CharacterArray, not just String.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

Rick Flower-2
On 17.10.2012 00:53, Paolo Bonzini wrote:

[...]
> That's slightly heavy artillery. :)

I was thinking the same thing.. but, hey.. options are options!! :-)
>
> Gwen has just beaten me to the answer, the method you want is
> #startsWith:.

Awesome -- not sure how I missed that one.. will check it out!  Thx!!

> Remember to look for methods in CharacterArray, not just String.

Yeah, I was poking around in all of the String classes and their
respective parent classes to see what I could find.. Apparently I
didn't look hard enough!  Thanks all!

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

Rick Flower-2
By the way.. I added a few methods that I found very helpful in my text
processing I've been doing lately.. Perhaps someone else might find
these
of interest -- they're modeled after similar methods already present
but
these work for stings instead of single characters.  Enjoy!

Stream extend [
        skipToString: aString [
        <comment: 'Skip to the specified string in the file -- must match the
entire line!!'>
                [self atEnd] whileFalse: [
                        self nextLine = aString ifTrue: [^true]].
                ^false
        ]

        findSubstring: aString [
        <comment: 'Skip to the specified sub-string in the stream'>
                | string |
                [self atEnd] whileFalse: [
                        string := self nextLine.
                        (string includesSubstring: aString caseSensitive: false)
                        ifTrue: [
                                "match found.. back the stream up to the start of the line"
                                self position: (self position - string size - 1).
                                ^true
                        ]
                ].
                ^false
        ]

        upToString: aString [
                | nextLine ws |
                ws := WriteStream on: (self species new: 8).
                [self atEnd or: [(nextLine := self nextLine) = aString]]
                        whileFalse: [ws nextPutAll: nextLine. ws nextPut: Character nl].
                ^ws contents
        ]

        upUntilStringPrefixMissing: aString [
                | nextLine ws |
                ws := WriteStream on: (self species new: 8).
                [self atEnd or: [(nextLine := self nextLine) startsWith: aString]]
                        whileTrue: [ws nextPutAll: nextLine. ws nextPut: Character nl].
                ^ws contents
        ]
]


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

Paolo Bonzini-2
Il 17/10/2012 17:43, Rick Flower ha scritto:

> By the way.. I added a few methods that I found very helpful in my text
> processing I've been doing lately.. Perhaps someone else might find these
> of interest -- they're modeled after similar methods already present but
> these work for stings instead of single characters.  Enjoy!
>
> Stream extend [
>     skipToString: aString [
>     <comment: 'Skip to the specified string in the file -- must match
> the entire line!!'>
>         [self atEnd] whileFalse: [
>             self nextLine = aString ifTrue: [^true]].
>         ^false
>     ]
>
>     findSubstring: aString [
>     <comment: 'Skip to the specified sub-string in the stream'>
>         | string |
>         [self atEnd] whileFalse: [
>             string := self nextLine.
>             (string includesSubstring: aString caseSensitive: false)
>             ifTrue: [
>                 "match found.. back the stream up to the start of the line"
>                 self position: (self position - string size - 1).
>                 ^true
>             ]
>         ].
>         ^false
>     ]

Isn't this skipToAll:

?

>
>     upToString: aString [
>         | nextLine ws |
>         ws := WriteStream on: (self species new: 8).
>         [self atEnd or: [(nextLine := self nextLine) = aString]]
>             whileFalse: [ws nextPutAll: nextLine. ws nextPut: Character
> nl].
>         ^ws contents
>     ]

This is a bit like upToAll: but not quite, what about calling it
upToLine: instead?

>     upUntilStringPrefixMissing: aString [
>         | nextLine ws |
>         ws := WriteStream on: (self species new: 8).
>         [self atEnd or: [(nextLine := self nextLine) startsWith: aString]]
>             whileTrue: [ws nextPutAll: nextLine. ws nextPut: Character nl].
>         ^ws contents
>     ]

upToLineStartingWith:

Paolo

> ]
>
>
> _______________________________________________
> help-smalltalk mailing list
> [hidden email]
> https://lists.gnu.org/mailman/listinfo/help-smalltalk
>


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Best way to find if a string begins with a particular string..

Rick Flower-2
Thanks Paolo.. I will cogitate on this once I get back
to working on this stuff-- I've got some C++ to chew on
for now.. :-(

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk