Compare Strings in Kotlin

To Compare Strings in Kotlin, following are some of the possible ways :

Using “==” Operator

We shall use==  operator for comparing two Strings in Kotlin. According to the documentation of Equality in Kotlin,  ==  operator is used for Structural Equality.

a==b  is implicitly translated to  a?.equals(b) ?: (b === null)  by Kotlin language.

Make sure that the two valuesa  andb  are of type Strings. Else convert them to Strings before comparison.

ADVERTISEMENT

Example 1 – Compare Strings

In the following example, we will take two strings in a and b variables, and compare them using == operator.

example.kt

/**
 * Kotlin example to compare if two strings are equal
 */

fun main(args: Array<String>) {
    var a: String = "kotlin is easy"
    var b: String = "kotlin is" + " easy"
    if(a==b){
        println("Strings '$a' and '$b' are equal.")
    } else {
        println("Strings '$a' and '$b' are not equal.")
    }

    b = "Kotlin runs on JVM"
    if(a==b){
        println("Strings '$a' and '$b' are equal.")
    } else {
        println("Strings '$a' and '$b' are not equal.")
    }
}

Output

Strings 'kotlin is easy' and 'kotlin is easy' are equal.
Strings 'kotlin is easy' and 'Kotlin runs on JVM' are not equal.

Using compareTo() extension function

Kotlin provides compareTo() extension function to String.

The syntax of compareTo() function is

fun String.compareTo(
      other: String, 
      ignoreCase: Boolean = false
  ): Int

other: String is mandatory argument. ignoreCase is optional.

The function returns integer value.

Return ValueDescription
0The two strings are equal.
negative integerIf the string is less than the other string
positive integerIf the string is greater than the other string

Example 2 – Compare Strings – String.compareTo()

In the following example, we will compare two strings in Kotlin using the function String.compareTo()

example.kt

/**
 * Kotlin example to compare two strings
 */

fun main(args: Array<String>) {
    var a: String = "apple"
    var b: String = "apple"
    var result = a.compareTo(b)
    if(result==0){
        println("Strings '$a' and '$b' are equal.")
    } else if(result < 0){
        println("'$a' is less than '$b' lexically.")
    } else{
        println("'$a' is less than '$b' lexically.")
    }

    b = "banana"
    result = a.compareTo(b)
    if(result==0){
        println("Strings '$a' and '$b' are equal.")
    } else if(result < 0){
        println("'$a' is less than '$b' lexically.")
    } else{
        println("'$a' is less than '$b' lexically.")
    }

    // passing ignoreCase to compareTo
    a = "appLE"
    b = "aPple"
    println("\nIgnoring Case...")
    result = a.compareTo(b, true)  // ignoreCase = true
    if(result==0){
        println("Strings '$a' and '$b' are equal.")
    } else if(result < 0){
        println("'$a' is less than '$b' lexically.")
    } else{
        println("'$a' is less than '$b' lexically.")
    }
}

Output

Strings 'apple' and 'apple' are equal.
'apple' is less than 'banana' lexically.

Ignoring Case...
Strings 'appLE' and 'aPple' are equal.

Conclusion

In this Kotlin Tutorial – Compare Strings in Kotlin, we have learnt to compare two strings using == Operator and CompareTo() String function, with the help of example programs.