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 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:
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?
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework