Kotlin equivalent of Java's equalsIgnoreCase


Java provides a String method called equalsIgnoreCase() which helps developers to compare two strings on the basis of their content. This comparison is case-insensitive, that is, it ignores whether the strings are in uppercase or lowercase and just compares the string values. In this article, we will see how we can implement the same functionality in Kotlin.

Example – equalsIgnoreCase in Java

The following example demonstrates how equalsIgnoreCase() works in Java.

public class MyClass {
   public static void main(String args[]){
      String s1="TutorialsPoint";
      String s2="tutorialspoint";
     
      System.out.println("String 1: " + s1);
      System.out.println("String 2: " + s2);
     
      // Strings match as we ignore the case
      System.out.println("Strings match? : " + s1.equalsIgnoreCase(s2));
   }
}

Output

It will produce the following output −

String 1: TutorialsPoint
String 2: tutorialspoint
Strings match? : true

Example – equalsIgnoreCase in Kotlin

Now, let's check how we can implement the same concept in Kotlin.

fun main(args: Array<String>) {
   val t1 = "TutorialsPoint.com";
   val t2 = "TutorialsPoint";
   val t3 = "tutorialspoint";
   
   // false as the strings do not match
   println("String 1: " + t1)
   println("String 2: " + t2)
   println("String 3: " + t3)
   
   // comparing t1 and t2
   println("Strings 1 and 2 match? : " + t1.equals(t2, ignoreCase = true));
   
   // comparing t2 and t3
   // both the strings match as we ignore their case
   println("Strings 2 and 3 match? : " + t2.equals(t3, ignoreCase = true));
}

Output

It will produce the following output −

String 1: TutorialsPoint.com
String 2: TutorialsPoint
String 3: tutorialspoint
Strings 1 and 2 match? : false
Strings 2 and 3 match? : true

Updated on: 16-Mar-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements