Compare Strings in kotlin!!

Maheshwar Ligade
techwasti
Published in
3 min readOct 2, 2019

--

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 that's 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 the 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 the 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.

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.

For more such stories

Lets connect on Stackoverflow , LinkedIn , Facebook& Twitter.

--

--

Maheshwar Ligade
techwasti

Learner, Full Stack Developer, blogger, amateur #ML,#DL,#AI dev in the quantum moment. I run https://techwasti.com/ to post all my articles.