[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

String startswith/endswith in Ruby?

Dave Benjamin

11/18/2003 11:46:00 PM

Hi all,

I'm a Python user trying to learn a bit of Ruby. I was wondering if there's
a common way of writing "startswith" and "endswith" expressions for strings
in Ruby.

For instance:

s = 'Hello, world!'
s.startswith('Hello') >> true
s.endswith('!') >> true
s.endswith('asdf') >> false

I'd like to avoid having to calculate the length of the string I'm searching
for; I'd prefer not to have to write "s[0..4] == 'Hello'" or similar.

Thanks!
Dave

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :
7 Answers

gabriele renzi

11/19/2003 12:02:00 AM

0

il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin
<ramen@lackingtalent.com> ha scritto::

>Hi all,
>
>I'm a Python user trying to learn a bit of Ruby. I was wondering if there's
>a common way of writing "startswith" and "endswith" expressions for strings
>in Ruby.

http://extensions.rubyforge.org/rdoc/classes/S...

then click on starts_with and ends_with and you'll see the code.
BTW had'nt matz accepted to add these methods to the standard String
class?

Harry Ohlsen

11/19/2003 12:04:00 AM

0

Hi David,

> s = 'Hello, world!'
> s.startswith('Hello') >> true
> s.endswith('!') >> true
> s.endswith('asdf') >> false

How about this ...

s =~ /^Hello/ # => true
s =~ /!$/ # => true
s =~ /asdf$/ # => false

While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.

Hence, one can attack this kind of thing in a uniform manner.

Cheers,

Harry O.




Michael Neumann

11/19/2003 12:33:00 AM

0

On Wed, Nov 19, 2003 at 08:52:18AM +0900, Dave Benjamin wrote:
> Hi all,
>
> I'm a Python user trying to learn a bit of Ruby. I was wondering if there's
> a common way of writing "startswith" and "endswith" expressions for strings
> in Ruby.
>
> For instance:
>
> s = 'Hello, world!'
> s.startswith('Hello') >> true
> s.endswith('!') >> true
> s.endswith('asdf') >> false

Regular expressions are your friend:

s =~ /^Hello/
s =~ /!$/
s =~ /asdf$/

Regards,

Michael


Dave Benjamin

11/19/2003 12:35:00 AM

0

In article <6jclrv0c35l2tcq6n968lpao29trqn0sr6@4ax.com>, gabriele renzi wrote:
> il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin
><ramen@lackingtalent.com> ha scritto::
>
>>I'm a Python user trying to learn a bit of Ruby. I was wondering if there's
>>a common way of writing "startswith" and "endswith" expressions for strings
>>in Ruby.
>
> http://extensions.rubyforge.org/rdoc/classes/S...
>
> then click on starts_with and ends_with and you'll see the code.

Perfect, thanks!

> BTW had'nt matz accepted to add these methods to the standard String
> class?

Beats me, but it'd be a nice addition.

Thanks everyone for the quick replies. I hadn't thought of using regexes,
though they do seem like overkill for this sort of problem. Anyway, I asked
what people commonly do, and I got my answer. =)

Peace,
Dave

--
..:[ dave benjamin (ramenboy) -:- www.ramenfest.com -:- www.3dex.com ]:.
: d r i n k i n g l i f e o u t o f t h e c o n t a i n e r :

gabriele renzi

11/19/2003 7:49:00 AM

0

il Wed, 19 Nov 2003 09:03:54 +0900, Harry Ohlsen
<harryo@qiqsolutions.com> ha scritto::

>Hi David,
>

>While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.

well, IMO It is less error prone, may be implemented in a faster way,
and is more clear to the casual reader.

Plus, like every builtin it avoide reinventing the weel.
Did you noticed 4 people gave 3 different answers?


another quick way to do it :
if 'ciao'[/iao$/]

:)
>

Harry Ohlsen

11/19/2003 10:46:00 PM

0

gabriele renzi wrote:

>>While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.
>
>
> well, IMO It is less error prone, may be implemented in a faster way,

True. I must say, I was surprised there weren't built-in methods for these.

> and is more clear to the casual reader.

I guess what I was getting at is that regexes are such a useful tool that I would hope most Ruby programmers would be pretty conversant with them ... and the necessary regexes in this case are about as simple as you can get.

> Plus, like every builtin it avoide reinventing the weel.

Of course, they're not very big wheels :-).

> Did you noticed 4 people gave 3 different answers?

Although, there were really only two, since "s =~ /^Hello/" is syntactic sugar for "/^Hello/.matches(s)".

Ie, there were "use regexes" or "use this extension".

> another quick way to do it :
> if 'ciao'[/iao$/]

TMTOWTDI ... put it down to Matz providing perl compatibility :-).





nobu.nokada

11/27/2003 5:21:00 AM

0

Hi,

At Wed, 19 Nov 2003 08:52:18 +0900,
Dave Benjamin wrote:
> s = 'Hello, world!'
> s.startswith('Hello') >> true
> s.endswith('!') >> true
> s.endswith('asdf') >> false

I often use

class String
def startswith(sub)
rindex(sub, 0)
end
def endswith(sub)
index(sub, -sub.length)
end
end

--
Nobu Nakada