String Concatenation in Kotlin
On this page (12sections)
Introduction
String Concatenation 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.
Strings can be joined using the + operator. 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
- Strings can be joined using the + operator.
- The plusAssign operator += can append text to an existing string variable.
- Concatenation creates a new string because String is immutable.
Syntax
var result = str1 + str2
String Concatenation in Kotlin Example Program in Kotlin
fun main(args: Array<String>) {
val first = "Hello, "
val second = "Kotlin"
val message = first + second
println(message)
}
Sample Output
Hello, Kotlin
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 first = "Hello, "assigns or updates a value used later in the program. -
val second = "Kotlin"assigns or updates a value used later in the program. -
val message = first + secondassigns or updates a value used later in the program. -
The
println(message)statement writes a line to the console — this produces part of the sample output below. -
Strings can be joined using the + operator.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
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
- Strings can be joined using the + operator.
- The plusAssign operator += can append text to an existing string variable.
- Concatenation creates a new string because String is immutable.
- 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 Concatenation in Kotlin?
When should I use String Concatenation?
How is String Concatenation different from Java?
How do I practice this topic?
Related Tutorials
String Basics in Kotlin
Learn String Basics in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialString Templates in Kotlin
Learn String Templates in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial