Compare Strings in kotlin

Featured

String comparison in kotlin. Equality of String in kotlin. In this tutorial, we will discuss how to compare strings in kotlin?

1. Using “==” Operator:-

As we are aware every programming language gas equal to an operator ( == ) to compare two things. Kotlin also allows equal to equal to as a comparator operator. Let’s start with the “==” operator. This operator can be used to check if the strings are structurally equal. It’s the equivalent of using the equals method in Java. According to the documentation of Equality in Kotlin,  ==  operator is used for Structural Equality.

str1==str2  is implicitly translated to str1?.equals(str2) ?: (str2 === null)  by Kotlin language.

# main method to compare two string using == operator.
fun main(args: Array<String>) {
    var a: String = "kotlin is very easy"
    var b: String = "kotlin is very" + " easy"
    if(a==b){
        println("Strings '$a' and '$b' are equal.")
    } else {
        println("Strings '$a' and '$b' are not equal.")
    }
    # change the content
    b = "Kotlin runs on JVM:)"
    if(a==b){
        println("Strings '$a' and '$b' are equal.")
    } else {
        println("Strings '$a' and '$b' are not equal.")
    }
}

In the above example if you see we have defined string using string literals ‘kotlin is very easy’ so, both the content of the variables is the same and they are pointing to the same object thats why both are equal.

It’s the equivalent of using == in Java

When we initialize string values using String literals, they point to the same object. However, if we build a string separately, the variable will point to a separate object. Let us take one more example.

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

2. Using equals():-

In this section, we will explore equals() method to compare two string in kotlin. This method will compare string with case-sensitive nature. “kotlin” and “KoTlin” both are different.

val a = "JVM"
val b = "jvm"

a.equals(b) # this will be return false.

The equals method returns the same result as the “ == ” operator.

If you want to have a case insensitive comparison. Then just pass true as the second argument.

Case-insensitive string comparison in kotlin, we can use the equals method and pass true as the second argument.

a.equals(b, true) # this will be return true.

3. Using compareTo:-

Kotlin provides compareTo() extension function to String, to compare strings equality. Like equals method, compareTo method which can be used to compare the order of the two strings.

Syntax:-

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

The compareTo method also comes with an optional ignoreCase argument, like a equal method. But compareTo return int value not a boolean.

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
a.compareTo(b, true) # case-insensitive
b.compareTo(b)  #case sensitive.

4. Conclusion:-

In this tutorial we have discussed comparing strings in kotlin using equal to operator “==”, equals() method and compareTo(). Comparing strings in kotlin is easy and straight forward.

Leave a Reply