Kotlin – Compare Strings

Compare Strings in Kotlin

To compare strings in Kotlin, we can use Kotlin Relational Operators.

  • To check if two strings are equal, use Equal-to operator.
  • To check if a string is greater than another string, use Greater-than operator.
  • To check if a string is less than another string, i.e., the first string appears prior to another string, if sorted in ascending order, use Less-than operator.

In this tutorial, we will go through each of the scenarios, and finally write a function that compares two strings.

Examples

Check if two given strings are equal

In this example, we compare two strings and check if they are equal using Equal-to operator.

Kotlin Program

fun main() {
    val str1 = "apple"
    val str2 = "apple"
    if (str1 == str2) {
        println("The two strings are equal.")
    } else {
        println("The two strings are not equal.")
    }
}

Output

The two strings are equal.

Check if string str1 is greater than other string str2

In this example, we will take two strings: str1 and str2; and check if the string str1 is greater than the other string str2 using greater than operator.

Kotlin Program

fun main() {
    val str1 = "cherry"
    val str2 = "apple"
    if (str1 > str2) {
        println("First string is greater than second string.")
    } else {
        println("First string is not greater than second string.")
    }
}

Output

First string is greater than second string.

Check if given string is less than other string

In this example, we will take two strings: str1 and str2; and check if the string str1 is less than the other string str2 using less than operator.

Kotlin Program

fun main() {
    val str1 = "cherry"
    val str2 = "apple"
    if (str1 < str2) {
        println("First string is less than second string.")
    } else {
        println("First string is not less than second string.")
    }
}

Output

First string is not less than second string.

Write a function that compares two given strings

Now, we will write a function that accepts two strings as parameters and compares them. The function returns zero if the strings are equal, negative value if first string is less than the second string and positive value if first string is greater than the second string.

Function

fun compareStrings(str1: String, str2: String): Int {
    if (str1 == str2) {
        return 0
    } else if (str1 > str2) {
        return 1
    } else {
        return -1
    }
}

Let us write a Kotlin program that uses this function, and compares two strings.

Kotlin Program

fun compareStrings(str1: String, str2: String): Int {
    if (str1 == str2) {
        return 0
    } else if (str1 > str2) {
        return 1
    } else {
        return -1
    }
}

fun main() {
    val str1 = "cherry"
    val str2 = "apple"

    val result = compareStrings(str1, str2)
    if (result == 0) {
        println("The two strings are equal.")
    } else if (result > 0) {
        print("\"$str1\" is greater than \"$str2\".")
    } else {
        print("\"$str1\" is less than \"$str2\".")
    }
}

Output

"cherry" is greater than "apple".