Open In App

Scala String compareToIgnoreCase() method with example

Last Updated : 03 Oct, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The compareToIgnoreCase() method is utilized to compare two strings like compareTo method but here the case (upper or lowercase) difference is ignored while comparing.

Method Definition:int compareToIgnoreCase(String str)
Return Type:It is same as compareTo() method but here only case difference is ignored.

Example: 1#




// Scala program of compareToIgnoreCase()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a String
        val m1 = "Nidhi"
          
        // Applying compareToIgnoreCase() method
        val result = m1.compareToIgnoreCase("Nidhi")
          
        // Displays output
        println(result)
      
    }


Output:

0

Example #2:




// Scala program of compareToIgnoreCase()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a String
        val m1 = "Nidhi"
          
        // Applying compareToIgnoreCase() method
        val result = m1.compareToIgnoreCase("NiDhi")
          
        // Displays output
        println(result)
      
    }


Output:

0

Here, also both the strings are considered same even if there is a case difference.
Example #3:




// Scala program of compareToIgnoreCase()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
        // Creating a String
        val m1 = "Nidhi"
      
        // Applying compareToIgnoreCase() method
        val result = m1.compareToIgnoreCase("NiDh")
      
        // Displays output
        println(result)
      
    }


Output:

1


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads