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

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

Like this? :wink:

"Hello world".match /^Hello/   # => MatchData
"Hello world".match /^world/   # => nil
···

On Sat, 13 Sep 2003 06:07:24 +0900 Philip Mak pmak@aaanime.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


Ryan Pavlik rpav@mephle.com

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

You might get somewhere with this:

str = “Hello, world”
str[“Hello”] == 0 # → true

Dunno about :end_with? though.

Gavin

···

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

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

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)

A recent thread talks about using a global class to
tuck variables in rather than have them polute a
namespace. I think this is a good idea(and is one of
the reasons java simply does not allow it).

I keeping all my ‘globals’ in an xml file using
rexml… like db connects, prod flag, etc. Right now
this file is loading everytime (until I can figure out
how to cache it). I least i thot it was not caching…

I have are toolbar items in my global
xml. and when i’m outputting
them, i check to see of the client is logged on or off
and create, on the fly, an element accordingly, and
append it to items. However, if i refresh, i find that
these elements are accumulating. When i output the
rexml, indeed, they are accumulating but i do not know
why. It’s either looping over my logic(don’t know
how!) or it’s caching the xml file for some reason.

#Are we logged on? #Dynamically add an element to the globals for the toolbar #To show logon or off

eItem = REXML::Element.new ‘item’

if $sess.data[‘on’]to_i == 1 then

eItem.attributes['name'] = 'Logoff'

eItem.text    = "/?cnt=adm/logon/index&logoff=1"

else

eItem.attributes['name'] = 'Logon'

eItem.text    = "/?cnt=adm/logon/index"				

end

eRight =
$global.root.elements[‘Html/Toolbars/Main/Right’]

eRight << eItem

#s = ‘’
#$global.write(s,1)
#print CGI.escapeHTML(s)

print $html.formatToolbar eRight

As anyone experienced this. I would sure like to cache
it at some point but wish i were doing it on my own!

:stuck_out_tongue:

···

Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software

[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

···

On Friday, September 12, 2003, at 05:20 PM, Philip Mak wrote:

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: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

“Mark Wilson” mwilson13@cox.net schrieb im Newsbeitrag
news:319AF784-E56A-11D7-BBD2-000393876156@cox.net

[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
···

On Friday, September 12, 2003, at 05:20 PM, Philip Mak 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.

Regards,

Mark

···

On Friday, September 12, 2003, at 08:42 PM, dblack@superlink.net 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.

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

···

On Monday, September 15, 2003, at 05:26 AM, Robert Klemme wrote:

“Mark Wilson” mwilson13@cox.net schrieb im Newsbeitrag
news:319AF784-E56A-11D7-BBD2-000393876156@cox.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]

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

Gavin

···

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.