Home »
Ruby »
Ruby Programs
Ruby program to check a string contains a specified substring or not
Ruby Example: Write a program to check a string contains a specified substring or not.
Submitted by Nidhi, on January 02, 2022
Problem Solution:
In this program, we will create two strings and check a string contains another string or not using the include?() method and print the appropriate message.
Program/Source Code:
The source code to check a string contains a specified substring or not is given below. The given program is compiled and executed successfully.
# Ruby program to check a string contains
# a specified substring or not
str = "Hello India";
substr="India";
if str.include?(substr)
puts "String contains specified substring.";
else
puts "String does not contain specified substring.";
end
Output:
String contains specified substring.
Explanation:
In the above program, we created two string variables str, substr that are initialized with "Hello India", "India". Then we used include?() method to check string str contains string substr and print the appropriate message.
Ruby Strings Programs »