Zach Dennis
11/19/2003 12:03:00 AM
Dave,
There is a common way of writing this. It uses the built-in regular
expression(s) of ruby.
Try something like.
s = "Hello, world!"
/^Hello/.match( s ) >> true
/!$/.match( s ) >> true
/asdf$/match( s ) >> false
HTH,
Zach
-----Original Message-----
From: Dave Benjamin [mailto:ramen@lackingtalent.com]
Sent: Tuesday, November 18, 2003 6:52 PM
To: ruby-talk ML
Subject: String startswith/endswith in Ruby?
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 :