×

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 compare two strings by ignoring case

Last Updated : December 15, 2025

Problem Solution

In this program, we will create four string variables and compare them using casecmp?() and print the appropriate message. The casecmp?() method is used to compare two strings by ignoring the case. It returns 0 if both strings are equal otherwise it returns a non-zero value.

Program/Source Code

The source code to compare two strings by ignoring the case is given below. The given program is compiled and executed successfully.

# Ruby program to compare two strings 
# by ignoring case

Str1 = "Hello";
Str2 = "hello";
Str3 = "Hello";
Str4 = "Hi";

if (Str1.casecmp(Str2)==0)
    print "Str1 and Str2 are equal\n";
else
    print "Str1 and Str2 are not equal\n";
end

if (Str1.casecmp(Str3)==0)
    print "Str1 and Str3 are equal\n";
else
    print "Str1 and Str3 are not equal\n";
end

if (Str1.casecmp(Str4)==0)
    print "Str1 and Str4 are equal\n";
else
    print "Str1 and Str4 are not equal\n";
end

Output

Str1 and Str2 are equal
Str1 and Str3 are equal
Str1 and Str4 are not equal

Explanation

In the above program, we created 4 variables Str1, Str2, Str3, Str4 that are initialized with "Hello", "hello", "Hello", "Hi".  Then we compared string variables using the casecmp?() method and printed the appropriate message.

Ruby Strings Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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