Test for string in string

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

On 8/14/06, cman [email protected] wrote:

Is there an easy way to test if a substring exists in a string?

I’m using this check:
if mystring.index(substring) > 0

index 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

cman wrote:

Is there an easy way to test if a substring
exists in a string?

I’m using this check:
if mystring.index(substring) > 0

if mystring.include?(“substring”)

end

HTH,
Bill

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs