How to check whether a string contains a substring in Ruby?

I have a string variable with content as follows:

varMessage =   
            "hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n"


            "/my/name/is/balaji.so\n"
            "call::myFunction(int const&)\n"
            "void::secondFunction(char const&)\n"
             .
             .
             .
            "this/is/last/line/liobrary.so"

in above string i have to find a sub string i.e.

"hi/thsid/sdfhsjdf/dfjsd/sdjfsdn\n"


"/my/name/is/balaji.so\n"
"call::myFunction(int const&)\n"

How can I find it? I just need to determine whether the substring is present or not.


ANSWERS:


You can use the include? method:

my_string = "abcdefg"
if my_string.include? "cde"
   puts "String includes 'cde'"
end

You can use the String Element Reference method which is []

Inside the [] can either be a literal substring, an index, or a regex:

> s='abcdefg'
=> "abcdefg"
> s['a']
=> "a"
> s['z']
=> nil

Since nil is functionally the same as false and any substring returned from [] is true you can use the logic as if you use the method .include?:

0> if s[sub_s]
1>    puts "\"#{s}\" has \"#{sub_s}\""
1> else 
1*    puts "\"#{s}\" does not have \"#{sub_s}\""
1> end
"abcdefg" has "abc"

0> if s[sub_s]
1>    puts "\"#{s}\" has \"#{sub_s}\""
1> else 
1*    puts "\"#{s}\" does not have \"#{sub_s}\""
1> end
"abcdefg" does not have "xyz" 

Just make sure you don't confuse an index with a sub string:

> '123456790'[8]    # integer is eighth element, or '0'
=> "0"              # would test as 'true' in Ruby
> '123456790'['8']  
=> nil              # correct

You can also use a regex:

> s[/A/i]
=> "a"
> s[/A/]
=> nil

Ternary way

my_string.include?('ahr') ? (puts 'String includes ahr') : (puts 'String does not include ahr')

OR

`puts (my_string.include?('ahr') ? 'String includes ahr' : 'String not includes ahr')`

user_input = gets.chomp
user_input.downcase!

if user_input.include?('substring')
  # Do something
end

This will help you check if the string contains substring or not

puts "Enter a string"
user_input = gets.chomp  # Ex: Tommy
user_input.downcase!    #  tommy


if user_input.include?('s')
    puts "Found"
else
    puts "Not found"
end

Expanding on Clint Pachl's answer:

Regex matching in Ruby returns nil when the expression doesn't match. When it does, it returns the index of the character where the match happens. For example

"foobar" =~ /bar/  # returns 3
"foobar" =~ /foo/  # returns 0
"foobar" =~ /zzz/  # returns nil

It's important to note that in Ruby only nil and the boolean expression false evaluate to false. Everything else, including an empty array, empty hash, or the integer 0, evaluates to true.

That's why the /foo/ example above works, and why

if "string" =~ /regex/

works as expected. Only entering the 'true' part of the if block if a match occurred.


A more succinct idiom than the accepted answer above that's available in Rails (from 3.1.0 and above) is .in?.

E.g:

my_string = "abcdefg"
if "cde".in? my_string
  puts "'cde' is in the String."
  puts "i.e. String includes 'cde'"
end

I also think it's more readable.

c.f.


You can also do this...

my_string = "Hello world"

if my_string["Hello"]
  puts 'It has "Hello"'
else
  puts 'No "Hello" found'
end

# => 'It has "Hello"'

If case is irrelevant, then a case-insensitive regular expression is a good solution:

'aBcDe' =~ /bcd/i  # evaluates as true

This will also work for multi-line strings.

See Ruby's Regexp class.



 MORE:


 ? How to check whether a string contains a substring in Ruby?
 ? How to check whether a string contains a substring in Ruby?
 ? Best way to find a substring in a string
 ? Equivalent python 'in' on ruby
 ? Ruby Is a string present within another string (using a regexp)?
 ? How to count a string elements' occurrence in another string in ruby?
 ? Convert String to Regexp in Ruby
 ? Array includes string ruby?
 ? How do I check if a string contains a specific word?
 ? How do I check if a string contains a specific word?