Q:

Ruby program to check a string contains a specified substring or not

belongs to collection: Ruby Strings Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 strsubstr 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

total answers (1)

Ruby Strings Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Ruby program to get the index of the substring in ... >>
<< Ruby program to demonstrate the string interpolati...