HiveBrain v1.2.0
Get Started
← Back to all entries
snippetrubyCritical

How to check whether a string contains a substring in Ruby

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
howcheckwhethersubstringrubystringcontains

Problem

I have a string variable with content:

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 the string I have to find a sub-string:

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

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


How can I find it? I need to determine whether the sub-string is present or not.

Solution

You can use the include? method:

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

Code Snippets

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

Context

Stack Overflow Q#8258517, score: 1630

Revisions (0)

No revisions yet.