Kotlin String Concatenation

To concatenate strings in Kotlin, you can use concatenation operator +. You may usually encounter concatenation operator as addition operator.

You may need to append or concatenate strings more often, when you are working with strings. Because strings are very commonly used datatypes in business logic. Most of times, data transfer happens from one application to another, as strings. So, you end up parsing the strings and at times joining or adding two or more strings.

In this tutorial, we shall learn how to concatenate strings in Kotlin programming language.

Following is the syntax to concatenate two strings.

var string_result = string_1 + string_2

The addition operator concatenates the two opearands string1 and string2 and returns the resulting string. You may store the result in a variable. In the above code snippet, we store the returned value in the variable string_result. In other words, a new string is created with string2 appended to string1.

Concatenation Operator allows chaining. This results in an easy way where you can concatenate two or more strings in a single statement. We shall go through an example that demonstrates this scenario.

Example 1 – Kotlin String Concatenation

In this example, we shall take two strings and perform string concatenation on them.

Kotlin Program – example.kt

/**
 * Kotlin - String Concatenation
 */
fun main(args: Array<String>) {
    //a string
    var string1 = "https://"
    //another string
    var string2 = "www.tutorialkart.com"
    //concatenate strings
    var stringResult = string1 + string2

    print(stringResult)
}

Run the Kotlin program.

Output

https://www.tutorialkart.com

The original strings are unaffected by this concatenation operation.

Example 2 – Kotlin String Concatenation – More than Two Strings

You can concatenate more than two strings in a single statement.

In this example, we shall take three strings and perform string concatenation on them. The process should be same for any number of string you would like concatenate.

Kotlin Program – example.kt

/**
 * Kotlin - String Concatenation
 */
fun main(args: Array<String>) {
    //a string
    var string1 = "https://"
    //another string
    var string2 = "www.tutorialkart.com"
    //yet another string
    var string3 = "/kotlin-tutorial/"
    //concatenate strings
    var stringResult = string1 + string2 + string3

    print(stringResult)
}

Run the Kotlin program.

Output

https://www.tutorialkart.com/kotlin-tutorial/

The order in which the strings are concatenated is determined by the order in which we provide the operands to the concatenation operator.

Example 3 – Kotlin String Concatenation – With Integer, Float, etc

You can concatenate other datatypes values to a string using the same concatenation operator.

In this example, we shall take a string and concatenate an int, float, double, boolean, etc., and find the resulting strings.

Kotlin Program – example.kt

/**
 * Kotlin - String Concatenation
 */
fun main(args: Array<String>) {
    // string + int
    println("tutorialkart-" + 2020)

    // string + float
    println("tutorialkart-" + 2020.05f)

    // string + boolean
    println("tutorialkart-" + true)
}

Run the Kotlin program.

Output

tutorialkart-2020
tutorialkart-2020.05
tutorialkart-true

Implicitly, Kotlin promotes the values with datatypes like int, float, boolean, etc., to String and then the concatenation happens.

Conclusion

In this Kotlin Tutorial, we learned how to concatenate Strings in Kotlin, with the help of Kotlin example programs.