Kotlin:
Concatenating strings

How to:

Here’s how to make strings stick together in Kotlin - no glue needed:

fun main() {
    val firstName = "Jet"
    val lastName = "Brains"
    val company = "Kotlin"

    // Using the plus operator
    val fullName = firstName + " " + lastName 
    println(fullName) // Output: Jet Brains

    // Using string templates
    val employeeIntro = "Hi, I'm $firstName and I work at $company."
    println(employeeIntro) // Output: Hi, I'm Jet and I work at Kotlin.

    // Using the concat() function
    val product = "IntelliJ IDEA"
    val description = " is awesome!"
    println(product.concat(description)) // Output: IntelliJ IDEA is awesome!
}

Deep Dive

Concatenation has been around as long as we’ve had strings to tie together. Programming languages have constantly evolved the way they handle this task. In the early days, you’d find walls of text being added together with a simple + operator. Fast forward to modern Kotlin, and you’ve got templates with $ symbols that pull variables right into the string, like magic.

Alternatives abound. If performance is key and you’re dealing with a truckload of strings, StringBuilder can be your best friend, avoiding creation of multiple string objects. Then there’s the joinToString function which takes a list and mushes it together separated by a delimiter of your choice.

Each method has its quirks—plus is easy but can be slow when overused; string templates are neat for readability; concat() harks back to Java’s method and feels a bit formal; StringBuilder and joinToString are more performant for lengthy operations.

See Also

Dive deeper into the world of Kotlin strings: