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.
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.
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.
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer