String startswith/endswith in Ruby?

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default 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) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
    : 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 :
    Dave Benjamin Guest

  2. Similar Questions and Discussions

    1. behaviour change of String#gsub(pattern) {|m| ... } for ruby 1.9/ruby2?
      String#gsub(pattern) {|m| ... } It really would be nice to get match data in 'm', but this would surely break _a lot_ of scripts. How about...
    2. Ruby idiom for all matches in a string
      What's the ruby idiom for all the matches in a string. x="ABA ABBBA CBBA" /A(B+)/.match(x) Ultimately, I want to get Thanks -- David...
    3. Ruby idiom for all matches in a string<Pine.LNX.4.44.0310122003300.6283-100000@ool-4355dfae.dyn.optonline.net>
      Hi -- On Mon, 13 Oct 2003, Gavin Sinclair wrote: That won't do it; it only gets the first one. To get them all, you can use String#scan: ...
    4. String length/concat problems in Ruby 1.8.0?
      Has anyone run into Ruby string length/concatenation problems in Ruby 1.8.0? One of my associates encountered difficulties when storing the...
    5. [ANN] ruby-freedb, ruby-serialport, ruby-mp3info moved to Rubyforge
      http://ruby-freedb.rubyforge.org/ http://ruby-serialport.rubyforge.org/ http://ruby-mp3info.rubyforge.org/ bye! --...
  3. #2

    Default Re: String startswith/endswith in Ruby?

    il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin
    <ramen@lackingtalent.com> ha scritto::
    >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.
    [url]http://extensions.rubyforge.org/rdoc/classes/String.html[/url]

    then click on starts_with and ends_with and you'll see the code.
    BTW had'nt matz accepted to add these methods to the standard String
    class?
    gabriele renzi Guest

  4. #3

    Default Re: String startswith/endswith in Ruby?

    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) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
    : 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 :



    Zach Dennis Guest

  5. #4

    Default Re: String startswith/endswith in Ruby?

    Hi David,
    > s = 'Hello, world!'
    > s.startswith('Hello') >> true
    > s.endswith('!') >> true
    > s.endswith('asdf') >> false
    How about this ...

    s =~ /^Hello/ # => true
    s =~ /!$/ # => true
    s =~ /asdf$/ # => false

    While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.

    Hence, one can attack this kind of thing in a uniform manner.

    Cheers,

    Harry O.




    Harry Ohlsen Guest

  6. #5

    Default Re: String startswith/endswith in Ruby?

    On Wed, Nov 19, 2003 at 08:52:18AM +0900, Dave Benjamin wrote:
    > 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
    Regular expressions are your friend:

    s =~ /^Hello/
    s =~ /!$/
    s =~ /asdf$/

    Regards,

    Michael


    Michael Neumann Guest

  7. #6

    Default Re: String startswith/endswith in Ruby?

    In article <6jclrv0c35l2tcq6n968lpao29trqn0sr6@4ax.com>, gabriele renzi wrote:
    > il Tue, 18 Nov 2003 23:45:43 -0000, Dave Benjamin
    ><ramen@lackingtalent.com> ha scritto::
    >
    >>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.
    >
    > [url]http://extensions.rubyforge.org/rdoc/classes/String.html[/url]
    >
    > then click on starts_with and ends_with and you'll see the code.
    Perfect, thanks!
    > BTW had'nt matz accepted to add these methods to the standard String
    > class?
    Beats me, but it'd be a nice addition.

    Thanks everyone for the quick replies. I hadn't thought of using regexes,
    though they do seem like overkill for this sort of problem. Anyway, I asked
    what people commonly do, and I got my answer. =)

    Peace,
    Dave

    --
    ..:[ dave benjamin (ramenboy) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
    : 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 :
    Dave Benjamin Guest

  8. #7

    Default Re: String startswith/endswith in Ruby?

    il Wed, 19 Nov 2003 09:03:54 +0900, Harry Ohlsen
    <harryo@qiqsolutions.com> ha scritto::
    >Hi David,
    >
    >While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.
    well, IMO It is less error prone, may be implemented in a faster way,
    and is more clear to the casual reader.

    Plus, like every builtin it avoide reinventing the weel.
    Did you noticed 4 people gave 3 different answers?


    another quick way to do it :
    if 'ciao'[/iao$/]

    >
    gabriele renzi Guest

  9. #8

    Default Re: String startswith/endswith in Ruby?

    gabriele renzi wrote:
    >>While using a regex to do something so simple seems a bit excessive, looking for a value at the start or end of a string is really just a special case of a more general problem, which regex matching solves very neatly.
    >
    >
    > well, IMO It is less error prone, may be implemented in a faster way,
    True. I must say, I was surprised there weren't built-in methods for these.
    > and is more clear to the casual reader.
    I guess what I was getting at is that regexes are such a useful tool that I would hope most Ruby programmers would be pretty conversant with them ... and the necessary regexes in this case are about as simple as you can get.
    > Plus, like every builtin it avoide reinventing the weel.
    Of course, they're not very big wheels :-).
    > Did you noticed 4 people gave 3 different answers?
    Although, there were really only two, since "s =~ /^Hello/" is syntactic sugar for "/^Hello/.matches(s)".

    Ie, there were "use regexes" or "use this extension".
    > another quick way to do it :
    > if 'ciao'[/iao$/]
    TMTOWTDI ... put it down to Matz providing perl compatibility :-).





    Harry Ohlsen Guest

  10. #9

    Default Re: String startswith/endswith in Ruby?

    Harry Ohlsen wrote:
    >>TMTOWTDI ... put it down to Matz providing perl compatibility :-).
    I just had my 21 bday. I'm putting down a few for Matz.

    Zach

    Zach Dennis Guest

  11. #10

    Default Re: String startswith/endswith in Ruby?

    Zach Dennis wrote:
    > I just had my 21 bday. I'm putting down a few for Matz.
    Congratulations! Have a couple for me!

    I'll have one for you later today; it's bloody hot here in Sydney today, so that won't be a problem :-).

    Cheers,

    Harry O.




    Harry Ohlsen Guest

  12. #11

    Default Re: String startswith/endswith in Ruby?

    In article <AKEKIKLMCFIHPEAHKAAICEIFGOAA.zdennis@mktec.com> , Zach Dennis wrote:
    > Harry Ohlsen wrote:
    >
    >>>TMTOWTDI ... put it down to Matz providing perl compatibility :-).
    >
    > I just had my 21 bday. I'm putting down a few for Matz.
    Cheers!

    --
    ..:[ dave benjamin (ramenboy) -:- [url]www.ramenfest.com[/url] -:- [url]www.3dex.com[/url] ]:.
    : 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 :
    Dave Benjamin Guest

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139