Scala String equalsIgnoreCase() Method with Example

Here, we will learn about the equalsIgnoreCase() method of String class in Scala. It is used to compare string objects. We will see its working, syntax and example.
Submitted by Shivang Yadav, on February 04, 2021

String is an immutable collection that stores sequences of characters.

String equalsIgnoreCase() Method

The equalsIgnoreCase() method on strings is used to compare string and object. The method ignores cases of characters of the string while making the comparison.

Syntax:

string1_Name.equalsIgnoreCase(String string2_Name)

Parameters: The method accepts a single parameter which the other string object to be compared.

Return Value: It returns a Boolean value which is the result of the comparison operation.

Example 1:

object MyClass {
    def main(args: Array[String]) {
        val string1 = "Scala"
        val string2 = "scaLa"
        
        val is_equal = string1.equalsIgnoreCase(string2)
    
        if(is_equal)
            println("The strings are equal!")
        else 
            println("The strings are not equal!")
    }
}

Output:

The strings are equal!

Example 2:

object MyClass {
    def main(args: Array[String]) {
        val string1 = "Scala"
        val string2 = "Scala programming "
        
        val is_equal = string1.equalsIgnoreCase(string2)
        
        if(is_equal)
            println("The strings are equal!")
        else 
            println("The strings are not equal!")
    }
}

Output:

The strings are not equal!


Comments and Discussions!

Load comments ↻





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