Professional Web Applications Themes

String#start_with? / #end_with? - Ruby

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 [url]http://www.rubygarden.org/article.php?sid=312[/url] suggests that such a method was suggested for inclusion into Ruby, but I don't see it in version 1.8.0....

  1. #1

    Default String#start_with? / #end_with?

    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

    [url]http://www.rubygarden.org/article.php?sid=312[/url] suggests that such a
    method was suggested for inclusion into Ruby, but I don't see it in
    version 1.8.0.

    Philip Mak Guest

  2. #2

    Default Re: String#start_with? / #end_with?

    On Sat, 13 Sep 2003 06:07:24 +0900
    Philip Mak <pmakaaanime.net> wrote:
    > 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

    --
    Ryan Pavlik <rpavmephle.com>

    "Must use last ounce of strength to stab from beyond the grave." - 8BT

    Ryan Pavlik Guest

  3. #3

    Default Re: String#start_with? / #end_with?

    Ryan Pavlik wrote:
    > "Hello world".match /^Hello/ # => MatchData
    > "Hello world".match /^world/ # => nil
    The solution for matching using regexps isn't that simple. It's more
    like this:

    "Hello world".match /^#{Regexp.escape('hello')}/

    because if the substring I'm checking for contains characters that
    would be considered special in a regexp, then they must be escaped or
    else it'll cause a (possibly silent) error. So I don't like this
    solution so much since it essentially amounts to typing:

    big_string.match(/^#{Regexp.escape(little_string)}/)

    instead of simply:

    big_string.start_with?(little_string)

    Philip Mak Guest

  4. #4

    Default Re: String#start_with? / #end_with?


    On Friday, September 12, 2003, at 05:20 PM, Philip Mak wrote:
    > [snip]
    class String

    def start_with?(little_string)
    !self.match(/^#{Regexp.escape(little_string)}/).nil?
    end

    end
    > [snip]
    >
    > big_string.start_with?(little_string)
    >
    Regards,

    Mark


    Mark Wilson Guest

  5. #5

    Default Re: String#start_with? / #end_with?

    Hi --

    On Sat, 13 Sep 2003, Mark Wilson wrote:
    >
    > On Friday, September 12, 2003, at 05:20 PM, Philip Mak wrote:
    >
    > > [snip]
    >
    > class String
    >
    > def start_with?(little_string)
    > !self.match(/^#{Regexp.escape(little_string)}/).nil?
    > end
    >
    > end
    >
    > > [snip]
    > >
    > > big_string.start_with?(little_string)
    All of the snippets in this thread will run aground on the fact that ^
    anchors to start-of-line, not start-of-string. You'd want to use the
    \A anchor for the latter.


    David

    --
    David Alan Black
    home: [email]dblacksuperlink.net[/email]
    work: [email]blackdavshu.edu[/email]
    Web: [url]http://pirate.shu.edu/~blackdav[/url]


    dblack@superlink.net Guest

  6. #6

    Default Re: String#start_with? / #end_with?


    On Friday, September 12, 2003, at 08:42 PM, [email]dblacksuperlink.net[/email] wrote:
    > [snip]
    > class String
    >
    > def start_with?(little_string)
    > !self.match(/^#{Regexp.escape(little_string)}/).nil?
    > end
    >
    > end
    >
    > [snip]
    >
    > big_string.start_with?(little_string)
    > All of the snippets in this thread will run aground on the fact that ^
    > anchors to start-of-line, not start-of-string. You'd want to use the
    > \A anchor for the latter.
    !self.match(/\A#{Regexp.escape(little_string)}/).nil?

    I think this, and the corresponding 'end_with?' method, is now a good
    candidate for the String section of the Standard Class Extensions
    category on the RubyGarden Wiki.

    Regards,

    Mark


    Mark Wilson Guest

  7. #7

    Default Re: String#start_with? / #end_with?

    On Saturday, September 13, 2003, 7:07:24 AM, Philip wrote:
    > 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
    > [url]http://www.rubygarden.org/article.php?sid=312[/url] 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


    Gavin Sinclair Guest

  8. #8

    Default Re: String#start_with? / #end_with?

    On Saturday, September 13, 2003, 10:52:35 AM, Mark wrote:
    > !self.match(/\A#{Regexp.escape(little_string)}/).nil?
    > I think this, and the corresponding 'end_with?' method, is now a good
    > candidate for the String section of the Standard Class Extensions
    > category on the RubyGarden Wiki.

    Done. I added an extra implementation which at first blush appears to
    give better performance.

    Gavin


    Gavin Sinclair Guest

  9. #9

    Default Re: String#start_with? / #end_with?


    "Mark Wilson" <mwilson13cox.net> schrieb im Newsbeitrag
    news:319AF784-E56A-11D7-BBD2-000393876156cox.net...
    >
    > On Friday, September 12, 2003, at 05:20 PM, Philip Mak wrote:
    >
    > > [snip]
    >
    > class String
    >
    > def start_with?(little_string)
    > !self.match(/^#{Regexp.escape(little_string)}/).nil?
    > end
    Would you care to explain why you test for nil? Why not:

    def start_with?(little_string)
    match(/^#{Regexp.escape(little_string)}/)
    end

    or even

    def start_with?(little_string)
    little_string.empty? || match(/^#{Regexp.escape(little_string)}/)
    end

    Regards

    robert

    Robert Klemme Guest

  10. #10

    Default Re: String#start_with? / #end_with?


    On Monday, September 15, 2003, at 05:26 AM, Robert Klemme wrote:
    >
    > "Mark Wilson" <mwilson13cox.net> schrieb im Newsbeitrag
    > news:319AF784-E56A-11D7-BBD2-000393876156cox.net...
    >>
    >> On Friday, September 12, 2003, at 05:20 PM, Philip Mak wrote:
    >>
    >>> [snip]
    >>
    >> class String
    >>
    >> def start_with?(little_string)
    >> !self.match(/^#{Regexp.escape(little_string)}/).nil?
    >> end
    >
    > Would you care to explain why you test for nil? Why not:
    >
    > def start_with?(little_string)
    > match(/^#{Regexp.escape(little_string)}/)
    > end
    >
    > or even
    >
    > def start_with?(little_string)
    > little_string.empty? || match(/^#{Regexp.escape(little_string)}/)
    > end
    > [snip]
    Totally mundane reasons -- I was extending Ryan Pavlik's initial
    suggestion in light of Philip Mak's further question. I chose to test
    for nil? so that the method returned true or false. Incidentally, I
    like Gavin Sinclair's alternate version on the RubyGardenWiki for
    character matching as opposed to Regexp matching plus escaping Regexp
    special characters.

    Regards,

    Mark


    Mark Wilson Guest

Similar Threads

  1. Replies: 1
    Last Post: February 27th, 12:04 AM
  2. #7056 [Com]: Setting string variables to value starting with '<' cause string to be empty.
    By davidgjenkins at ntlworld dot com in forum PHP Development
    Replies: 1
    Last Post: October 27th, 06:54 AM
  3. Replies: 5
    Last Post: September 4th, 02:03 AM
  4. Replies: 2
    Last Post: August 16th, 10:26 AM
  5. Replies: 4
    Last Post: August 14th, 06:04 AM

Bookmarks

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