Hi, Is there an easy way to test if a substring exists in a string? I''m using this check: if mystring.index(substring) > 0 but it doesn''t seem to be working.... Thanks! -- Posted via http://www.ruby-forum.com/.
On 8/14/06, cman <cmalpeli@gmail.com> wrote:> Is there an easy way to test if a substring exists in a string? > > I''m using this check: > if mystring.index(substring) > 0index returns nil if it fails to find a match, not -1 like in Java. So you can just test for true/false. if mystring.index(substring) ... end -- James
I believe you should be able to use: mystring.index.include? substring That should return true of the string contains the substring Best Regards, Tamim cman wrote:> Hi, > > Is there an easy way to test if a substring exists in a string? > > I''m using this check: > if mystring.index(substring) > 0 > > but it doesn''t seem to be working.... > > Thanks! > >
cman wrote:> Hi, > > Is there an easy way to test if a substring exists in a string? > > I''m using this check: > if mystring.index(substring) > 0 > > but it doesn''t seem to be working.... > > Thanks!''foobar''.include? ''foo'' #=> true ''foobar''.include? ''sup'' #=> false -- Posted via http://www.ruby-forum.com/.