[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

String#start_with? / #end_with?

Philip Mak

9/12/2003 9:07:00 PM

Is there a built-in method in Ruby for checking whether a string starts
with or ends with a pattern? e.g.:

"Hello, world".start_with?("Hello") => true
"Hello, world".start_with?("world") => false

http://www.rubygarden.org/article.p... suggests that such a
method was suggested for inclusion into Ruby, but I don't see it in
version 1.8.0.

2 Answers

Ryan Pavlik

9/12/2003 9:16:00 PM

0

On Sat, 13 Sep 2003 06:07:24 +0900
Philip Mak <pmak@aaanime.net> wrote:

> Is there a built-in method in Ruby for checking whether a string starts
> with or ends with a pattern? e.g.:
>
> "Hello, world".start_with?("Hello") => true
> "Hello, world".start_with?("world") => false
>

Like this? ;-)

"Hello world".match /^Hello/ # => MatchData
"Hello world".match /^world/ # => nil

--
Ryan Pavlik <rpav@mephle.com>

"Must use last ounce of strength to stab from beyond the grave." - 8BT

Gavin Sinclair

9/13/2003 12:55:00 AM

0

On Saturday, September 13, 2003, 7:07:24 AM, Philip wrote:

> Is there a built-in method in Ruby for checking whether a string starts
> with or ends with a pattern? e.g.:

> "Hello, world".start_with?("Hello") => true
> "Hello, world".start_with?("world") => false

> http://www.rubygarden.org/article.p... suggests that such a
> method was suggested for inclusion into Ruby, but I don''t see it in
> version 1.8.0.

You might get somewhere with this:

str = "Hello, world"
str["Hello"] == 0 # -> true

Dunno about :end_with? though.

Gavin