[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: String#start_with? / #end_with?

Philip Mak

9/12/2003 9:21:00 PM

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)