×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

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

Last Updated : December 15, 2025

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 »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.