ruby picture

RCR 150: String#start_with? / #end_with?

Submitted by anonymous (Thu Aug 21 14:08:56 UTC 2003)

Abstract

This is a legacy RCR from Ruby Garden, submitted by anonymous. Matz has declared these RCRs obsolete, and asked that their authors resubmit them in the new format.

I'd like the following to be in Standard Ruby:

class String
  def start_with?(other, length = other.length)
    self[0...length] == other[0...length]
  end  def end_with?(other, length = other.length)
    self[-length..-1] == other[-length..-1]
  end
end

This allows the more readable "foobar".start_with? "foo" and "barqux".end_with? "qux" instead of the more long winded and error prone "foobar"[0...3] == "foo" and "barqux"[-3..-1] == "qux" .

IMHO checking whether a String starts or ends with another String is done commonly so there should be a simple and clear way to do so.

Problem

(RCR imported from old format)

Proposal

Analysis

(RCR imported from old format)

Implementation

(RCR imported from old format)
ruby picture
Comments Current voting

Re: String#start_with? / #end_with? (, 2003-08-21 22:26:43)

How about:
str = "foobar"

if (str =~ /^foo/)
...
end

if (str =~ /bar$/)
...
end

It's not as easy as that... (, 2003-08-22 00:43:44)

The real solution which allows you to use arbitrary strings would have to be more like str =~ /#{Regexp::quote("a string...")}$/ and I don't think that's very idiomatic.

Re: It's not as easy as that... (, 2003-08-22 11:13:22)

You don't have to use Regexp::quote(...):

start_with = "foo"
end_with = "baz"
end_with2 = "bar baz"

str = "foo bar baz"

puts "#{str} starts with '#{start_with}'" if str =~ /^#{start_with}/
puts "#{str} starts with 'foo bar'" if str =~ /^foo bar/
puts "#{str} ends with '#{end_with}'" if str =~ /#{end_with}$/
puts "#{str} ends with '#{end_with2}'" if str =~ /#{end_with2}$/
puts "#{str} ends with 'bar baz'" if str =~ /bar baz$/

This will produce:
foo bar baz starts with 'foo'
foo bar baz starts with 'foo bar'
foo bar baz ends with 'baz'
foo bar baz ends with 'bar baz'
foo bar baz ends with 'bar baz'

You can even check start_with and end_with in one shot by something like:

str =~ /^foo.*?baz$/



Re: It's not as easy as that... (, 2003-08-22 19:02:00)

start_with = "*** make money fast ***"
str = "Hello World!"
puts "#{str} starts with '#{start_with}'" if str =~ /^#{start_with}/

produces this error for me:

RegexpError: invalid regular expression; there's no previous pattern, to which '*' would define cardinality at 2: /^*** make money fast ***/

So the Regexp::quote() is needed.

Re: It's not as easy as that... (, 2003-08-22 22:50:46)

Bummer...how did I miss that? I guess I never had a single case where the string contained a special character...but hey, it's not a bad thing that I learned something from somebody.

I wish I could change my previous vote to a YES, but (surprise!) the system won't allow me to re-vote/change...sorry about that... :-(

Re: It's not as easy as that... (, 2003-08-24 00:59:14)

Heh, that's no problem. We have 4 out of 5 positives and I guess matz actually reads the comments. :)

Re: String#start_with? / #end_with? (, 2003-09-02 19:50:23)

perhaps starts_with is a better name that start_with

see

Re: String#start_with? / #end_with? (, 2003-09-05 10:00:08)

That might be the case. -- I was just choosing start_with? because I wanted to stay consistent with some original methods. (mainly Array#include?)


Strongly opposed 0
Opposed 0
Neutral 0
In favor 0
Strongly advocate 0
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .