Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

equal
string
ruby
communitycreator

How to check if two strings are equal in Ruby

okwudili onyejiaku

Overview

In this shot, we will check if two strings are equal with the eql?() method. Two strings are equal if they both have the same length and content with the same case.

Syntax

str.eql?(other_str)

Parameters

other_str: The string we compare with the string instance.

Return value

The value returned is a Boolean. It returns the true if the strings are equal. Otherwise, false is returned.

Example

# compare strings
puts "one".eql?("one"); # true
puts "two".eql?("two"); # true
puts "three".eql?("three"); # true
puts "FIVE".eql?("five"); # false
puts "six".eql?("Six"); # false
Check if strings are equal in Ruby using eql?() method

Explanation

All the first three string comparisons return true because they are equal. The rest are not equal because they are not of the same case. That is why they will return false.

RELATED TAGS

equal
string
ruby
communitycreator
RELATED COURSES

View all Courses

Keep Exploring