String Comparison in Kotlin
On this page (12sections)
Introduction
String Comparison is a fundamental concept every Kotlin developer should understand. Strings appear in almost every program — user input, file paths, API responses, and UI labels. Kotlin’s standard library provides concise helpers for common text tasks.
Use == and != for structural (content) comparison of strings. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- Use == and != for structural (content) comparison of strings.
- Use === and !== for referential equality when needed.
- The compareTo function compares strings lexicographically.
Syntax
if (str1 == str2) { ... }
String Comparison in Kotlin Example Program in Kotlin
fun main(args: Array<String>) {
val a = "Kotlin"
val b = "Kotlin"
val c = String(charArrayOf('K','o','t','l','i','n'))
println("a == b : ${a == b}")
println("a == c : ${a == c}")
println("a === c : ${a === c}")
}
Sample Output
a == b : true
a == c : true
a === c : false
When to use
Use string functions when formatting output, validating user input, splitting CSV data, or building URLs and file paths.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
val a = "Kotlin"assigns or updates a value used later in the program. -
val b = "Kotlin"assigns or updates a value used later in the program. -
val c = String(charArrayOf('K','o','t','l','i','n'))assigns or updates a value used later in the program. -
The
println("a == b : ${a == b}")statement writes a line to the console — this produces part of the sample output below. -
The
println("a == c : ${a == c}")statement writes a line to the console — this produces part of the sample output below. -
The
println("a === c : ${a === c}")statement writes a line to the console — this produces part of the sample output below. -
Use == and != for structural (content) comparison of strings.
Best Practices
- Prefer string templates (
"Hello, $name") over concatenation for readability. - Use
trim(),isBlank(), andisEmpty()to validate user input consistently. - Choose
StringBuilderwhen building large strings inside loops.
Common Mistakes
- Calling
substringwith wrong end indices — prefersubstringAfter/substringBeforehelpers. - Using
==when case-insensitive comparison is needed — useequals(other, ignoreCase = true). - Concatenating inside tight loops instead of using
StringBuilder.
Key Points
- Use == and != for structural (content) comparison of strings.
- Use === and !== for referential equality when needed.
- The compareTo function compares strings lexicographically.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Semicolons at the end of statements are optional in Kotlin.
Frequently Asked Questions
What is String Comparison in Kotlin?
When should I use String Comparison?
How is String Comparison different from Java?
How do I practice this topic?
Related Tutorials
String Templates in Kotlin
Learn String Templates in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialString Length and Substring in Kotlin
Learn String Length and Substring in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial