Kotlin – Concatenate Strings

Concatenate Strings in Kotlin

To concatenate two or more strings in Kotlin, you can use + operator.

The following expression concatenates string values: str1 and str2, and returns the resulting concatenated string.

str1 + str2

To concatenate more than two string values, you can chain the + operator as shown in the following.

str1 + str2 + str3 + ... + strN

Examples

Concatenate “Hello ” with First name

In the following example, we take two strings. The first string greeting is a string literal "Hello ". The second string consists of the firstName of a user. We shall concatenate these two strings using + operator.

Kotlin Program

fun main() {
    val greeting = "Hello "
    val firstName = "Apple"
    val resultingString = greeting + firstName
    println(resultingString)
}

Output

Hello Apple

Concatenate three strings

In the following example, we take three strings and concatenate them using + operator.

Kotlin Program

fun main() {
    val str1 = "Apple"
    val str2 = "Banana"
    val str3 = "Cherry"
    val resultingString = str1 + str2 + str3
    println(resultingString)
}

Output

AppleBananaCherry