How to Compare Strings in Kotlin: Syntax and Examples
In Kotlin, you compare strings using
== for content equality or === for reference equality. You can also use equals() for case-sensitive comparison and compareTo() to check lexicographical order.Syntax
Here are the main ways to compare strings in Kotlin:
str1 == str2: Checks if the content of two strings is equal.str1 === str2: Checks if two string variables point to the exact same object.str1.equals(str2): Another way to check content equality (case-sensitive).str1.equals(str2, ignoreCase = true): Checks content equality ignoring case.str1.compareTo(str2): Compares strings lexicographically, returns 0 if equal, <0 if str1 < str2, >0 if str1 > str2.
kotlin
val str1 = "Hello" val str2 = "hello" // Content equality val isEqual = str1 == str2 // Reference equality val isSameObject = str1 === str2 // equals() with ignore case val isEqualIgnoreCase = str1.equals(str2, ignoreCase = true) // Lexicographical comparison val comparison = str1.compareTo(str2)
Example
This example shows how to compare strings using different methods and prints the results.
kotlin
fun main() {
val a = "Kotlin"
val b = "kotlin"
val c = a
println("a == b: ${a == b}") // false, content differs in case
println("a.equals(b, ignoreCase = true): ${a.equals(b, ignoreCase = true)}") // true
println("a === b: ${a === b}") // false, different objects
println("a === c: ${a === c}") // true, same object
val result = a.compareTo(b)
when {
result == 0 -> println("a and b are equal lexicographically")
result < 0 -> println("a comes before b")
else -> println("a comes after b")
}
}Output
a == b: false
a.equals(b, ignoreCase = true): true
a === b: false
a === c: true
a comes before b
Common Pitfalls
Many beginners confuse == and === in Kotlin. == checks if two strings have the same content, while === checks if they are the exact same object in memory.
Also, using equals() without specifying ignoreCase can cause unexpected false results if case differs.
kotlin
fun main() {
val x = "Test"
val y = "Test"
val z = String(charArrayOf('T', 'e', 's', 't'))
println(x == y) // true, content equal
println(x === y) // true, Kotlin may reuse string literals
println(x == z) // true, content equal
println(x === z) // false, different objects
// Wrong: expecting true but case differs
println(x.equals("test")) // false
// Correct: ignore case
println(x.equals("test", ignoreCase = true)) // true
}Output
true
true
true
false
false
true
Quick Reference
| Method | Description | Example |
|---|---|---|
| == | Checks if string contents are equal | "a" == "a" // true |
| === | Checks if two references point to the same object | "a" === "a" // usually true for string literals |
| equals() | Checks content equality, case-sensitive by default | "a".equals("A") // false |
| equals(ignoreCase = true) | Checks content equality ignoring case | "a".equals("A", ignoreCase = true) // true |
| compareTo() | Compares strings lexicographically | "a".compareTo("b") // negative number |
Key Takeaways
Use == to compare string contents for equality in Kotlin.
Use === only to check if two variables point to the same string object.
Use equals(ignoreCase = true) to compare strings ignoring case differences.
Use compareTo() to determine lexicographical order between strings.
Be careful not to confuse content equality (==) with reference equality (===).