In this tutorial, you shall learn how to check if given strings are equal ignoring case in Kotlin, using String.equals() function, with examples.

Kotlin – Check if strings are equal ignoring case

To check if strings are equal ignoring case in Kotlin, you can use String.equals() function.

Call the equals() function on the first string and pass the second string, and ignoreCase=true as arguments to the function. The function returns a boolean value true if the two strings are equal ignoring the case of the characters, or else, it returns false.

Syntax

The syntax to check if the strings str1 and str2 are equal ignoring the case is

</>
Copy
str1.equals(str2, ignoreCase=true)

Examples

In the following program, we take two strings in str1 and str2, and check if these two strings are equal, or not, ignoring the case.

Main.kt

</>
Copy
fun main() {
    val str1 = "hello"
    val str2 = "HeLLo"
    if (str1.equals(str2, ignoreCase = true)) {
        println("Two strings are equal ignoring case.")
    } else {
        println("Two strings are not equal ignoring case.")
    }
}

Output

Two strings are equal ignoring case.

Reference tutorials for the program

Conclusion

In this Kotlin Tutorial, we learned how to check if two strings are equal ignoring case using String.equals() function.