w3resource

Ruby String Exercises: Check whether a string contains a substring

Ruby String: Exercise-4 with Solution

Write a Ruby program to check whether a string contains a substring.

Ruby String Exercises: Check whether a string contains a substring

Ruby Code:

def check_string(my_string, my_substr)
    if my_string.include? my_substr
      return "Substring present in the string."
   else
      return "Substring not present in the string."
   end
end
print check_string("JavaScript","Script")
print "\n",check_string("JavaScript","Scriptt")

Output:

Substring present in the string.
Substring not present in the string.

Flowchart:

Flowchart: Check whether a string contains a substring

Ruby Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a Ruby program to lower case, upper case and capitalizes all the words of a given string.
Next: Write a Ruby program to remove all white space within a string.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/ruby-exercises/string/ruby-string-exercise-4.php