snippetrubyCritical
How to check whether a string contains a substring in Ruby
Viewed 0 times
howcheckwhethersubstringrubystringcontains
Problem
I have a string variable with content:
In the string I have to find a sub-string:
How can I find it? I need to determine whether the sub-string is present or not.
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'"
endCode Snippets
my_string = "abcdefg"
if my_string.include? "cde"
puts "String includes 'cde'"
endContext
Stack Overflow Q#8258517, score: 1630
Revisions (0)
No revisions yet.