PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Start of string?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Start of string?

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 12h30   #1
Jari Williamsson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Start of string?

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

  Réponse avec citation
Vieux 06/11/2007, 13h02   #2
David A. Black
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Start of string?

Hi --

On Tue, 6 Nov 2007, Jari Williamsson wrote:

> 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!

  Réponse avec citation
Vieux 06/11/2007, 13h07   #3
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Start of string?

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 s

Stefano

  Réponse avec citation
Vieux 06/11/2007, 13h08   #4
Golda Bence
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Start of string?

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

Jari Williamsson wrote:
> 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



  Réponse avec citation
Vieux 06/11/2007, 13h13   #5
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Start of string?

Jari,

> Is there any such solution?


Here is a regular expression one:

"longstring" =~ /^long/

HTH,
Peter
___
http://www.rubyrailways.com
http://scrubyt.org


  Réponse avec citation
Vieux 06/11/2007, 13h17   #6
Arlen Christian Mart Cuss
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Start of string?


On Tue, 2007-11-06 at 21:30 +0900, Jari Williamsson wrote:
> 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


How about;

if /^long/.match a

if a.match /^long/

if a.index('long').zero?

The last one probably has the best portability for your non-constant
keys.

Arlen


  Réponse avec citation
Vieux 06/11/2007, 15h55   #7
Nobuyoshi Nakada
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Start of string?

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.

--
Nobu Nakada

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h28.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11833 seconds with 15 queries