Start of string?

  • Thread starter Jari Williamsson
  • Start date
J

Jari Williamsson

What't the most elegant way to find a substring match at the very start
of a string?

Currently I'm using this approach:
a = "long string"
key = "long"
if a[0, key.length] == key then

...while this might look ok, it doesn't look so good with constant strings:
if a[0, 4] == "long" then
or
if a[0, "long".length] == "long" then

I guess what I'm looking for is something like:
if a.startswith("long") then

Is there any such solution?


Best regards,

Jari Williamsson
 
D

David A. Black

Hi --

What't the most elegant way to find a substring match at the very start of a
string?

Currently I'm using this approach:
a = "long string"
key = "long"
if a[0, key.length] == key then

...while this might look ok, it doesn't look so good with constant strings:
if a[0, 4] == "long" then
or
if a[0, "long".length] == "long" then

I guess what I'm looking for is something like:
if a.startswith("long") then

Is there any such solution?

You could do:

if a.index("long") == 0


David

--
Upcoming training by David A. Black/Ruby Power and Light, LLC:
* Advancing With Rails, Edison, NJ, November 6-9
* Advancing With Rails, Berlin, Germany, November 19-22
* Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details!
 
S

Stefano Crocco

Alle marted=EC 6 novembre 2007, Jari Williamsson ha scritto:
What't the most elegant way to find a substring match at the very start
of a string?

Currently I'm using this approach:
a =3D "long string"
key =3D "long"
if a[0, key.length] =3D=3D key then

...while this might look ok, it doesn't look so good with constant string= s:
if a[0, 4] =3D=3D "long" then
or
if a[0, "long".length] =3D=3D "long" then

I guess what I'm looking for is something like:
if a.startswith("long") then

Is there any such solution?


Best regards,

Jari Williamsson

You can use a regexp:

if a.match /^long/ then
=2E..

The ^ at the beginning of the regexp means that the regexp should be matche=
d=20
at the beginning of the string (actually, of the line. If your string is=20
multiline, you need to use \A to match the beginning of the string).

If the string you want to look for is stored in a variable, you can use str=
ing=20
interpolation inside the regexp:

str =3D "long"
if a.match /^#{str}/ then

Of course, this can cause problems if str contains characters which are=20
special in a regexp. In this case, you need to quote it:

str =3D "long"
if a.match /^#{Regexp.quote(str)}/ then


I hope this helps

Stefano
 
G

Golda Bence

Hi!

Take a look at Regexp class:

http://www.ruby-doc.org/core/classes/Regexp.html

...after that your starts_with? method should be similar to this one:

class String
def starts_with?(a_string)
self =~ Regexp.new('^%s' % [ a_string ])
end
end

a = "long string"
a.starts_with?("long") # => 0
a.starts_with?("not soo long") # => nil

!note: in ruby 0 (Fixnum) means true in a condition, so does ""
(String), but nil (NilClass) means false.

Cheers, Bence
 
N

Nobuyoshi Nakada

Hi,

At Tue, 6 Nov 2007 22:02:27 +0900,
David A. Black wrote in [ruby-talk:277691]:
You could do:

if a.index("long") == 0

rindex("long", 0) is faster for long but unmatching strings.
 

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,270
Messages
2,571,348
Members
48,034
Latest member
JaimieBarn

Latest Threads

Top