ADVERTISEMENT
ADVERTISEMENT

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 »


ADVERTISEMENT


ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.