String#start_with? / #end_with?

P

Philip Mak

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.php?sid=312 suggests that such a
method was suggested for inclusion into Ruby, but I don't see it in
version 1.8.0.
 
R

Ryan Pavlik

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
 
G

Gavin Sinclair

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.php?sid=312 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,125
Messages
2,570,748
Members
47,302
Latest member
MitziWragg

Latest Threads

Top