RCR 150: String#start_with? / #end_with?
  submitted by anonymous on 2003-08-21 10:56:56
  
  This is a legacy RCR. If this is your RCR, please resubmit it using the new format and process.
  
  
    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.
   
  
  
  Vote for this RCR
  
  
  
  
  Add comments here
  
  
    How about:
    str = "foobar"
    
    if (str =~ /^foo/)
    ...
    end
    
    if (str =~ /bar$/)
    ...
    end
  
  
  
    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.
  
  
  
    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$/
    
    
    
  
  
  
    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() <strong>is</strong> needed.
   
  
  
    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... :-(
  
  
  
    Heh, that's no problem. We have 4 out of 5 positives and I guess matz actually reads the comments. :)
  
  
  
    perhaps starts_with is a better name that start_with
    
    see
    
    http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#startsWith(java.lang.String)
  
  
  
    That might be the case. -- I was just choosing start_with? because I wanted to stay consistent with some original methods. (mainly Array#include?)
  
  Back to RCRchive.
  
  RCR Submission page and RCRchive powered by Ruby, Apache, RuWiki (modified), and RubLog
 
str = "foobar"
if (str =~ /^foo/)
...
end
if (str =~ /bar$/)
...
end