English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin String Comparison

There are several methods to compare strings in Kotlin:

1Using the '===' operator
2Using the compareTo() extension function

Compare strings using the '===' operator

We will use the === operator to compare two strings in Kotlin. According to the Kotlin Equality documentation, the === operator is used for structural equality.

In Kotlin language: a==b is implicitly converted to a?.equals(b) ?: (b === null)

Ensure that both values a and b are of String type. If they are not strings, they need to be converted to strings before comparison.

/**
* Kotlin example, comparing two strings for equality*/
 
fun main(args: Array<String>) {
    var a: String = "kotlin is easy"
    var b: String = "kotlin is" + "easy"
    if(a==b){
        println("The strings '$a' and '$b' are equal.")
    } else {
        println("The strings '$a' and '$b' are not equal.")
    }
 
    b = "Kotlin runs on JVM"
    if(a==b){
        println("The strings '$a' and '$b' are equal.")
    } else {
        println("The strings '$a' and '$b' are not equal.")
    }
}

Output Result:

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

Using the compareTo() extension function to compare strings

Kotlin provides the compareTo() extension function for String.
The syntax of the compareTo() function is as follows:

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

Other: String is a mandatory parameter. ignoreCase is optional.

This function returns an integer value.

Return valueDescription
0

These two strings are equal.

Negative integer

If the string is less than the other string

Positive integer

If the string is greater than the other string

/**
* Kotlin example, comparing two strings
*/
fun main(args: Array<String>) {
    var a: String = "apple"
    var b: String = "apple"
    var result = a.compareTo(b)
    if(result == 0)
        println("The strings '$a' and '$b' are equal")
    }
        println("'$a' is less than '$b' in character count.")
    }
        println("'$a' is less than '$b' in character count.")
    }
 
    b = "banana"
    result = a.compareTo(b)
    if(result == 0)
        println("The strings '$a' and '$b' are equal")
    }
        println("'$a' is less than '$b' in character count.")
    }
        println("'$a' is less than '$b' in character count.")
    }
 
    // compareTo ignores case, the ignoreCase parameter is true
    a = "appLE"
    b = "aPple"
    println("\nCase-insensitive comparison...")
    result = a.compareTo(b, true) // ignoreCase = true
    if(result == 0)
        println("The strings '$a' and '$b' are equal")
    }
        println("'$a' is less than '$b' in character count.")
    }
        println("'$a' is less than '$b' in character count.")
    }
}

Output Result:

The strings 'apple' and 'apple' are equal
The string 'apple' is less than 'banana' in character count.
 
Case-insensitive...
The strings 'appLE' and 'aPple' are equal

 
This chapter learns to compare two strings using the == operator and CompareTo() function with the help of sample programs.