Skip to main content

How to concatenate strings in Kotlin

How to concatenate strings in Kotlin.

Here is a detailed step-by-step tutorial on how to concatenate strings in Kotlin.

Introduction

Concatenation is the process of combining two or more strings into a single string. In Kotlin, there are multiple ways to concatenate strings, depending on the specific use case. In this tutorial, we will explore different methods to concatenate strings in Kotlin.

Using the Plus Operator (+)

The simplest and most common way to concatenate strings in Kotlin is by using the plus operator (+). This operator can be used to concatenate two or more strings together.

val str1 = "Hello"
val str2 = "World"

val result = str1 + " " + str2
println(result) // Output: Hello World

In the above example, we first declare two variables str1 and str2 with the values "Hello" and "World" respectively. The plus operator is then used to concatenate the two strings along with a space in between. The resulting string is stored in the result variable and printed to the console.

Using the String Template

Another way to concatenate strings in Kotlin is by using string templates. String templates allow you to embed expressions inside a string literal and evaluate them at runtime.

val name = "John"
val age = 25

val message = "My name is $name and I am $age years old."
println(message) // Output: My name is John and I am 25 years old.

In the above example, we have two variables name and age representing a person's name and age. The string template is used to concatenate the variables with the surrounding text. The expressions $name and $age are evaluated at runtime and replaced with their respective values.

Using the StringBuilder

If you need to concatenate multiple strings in a loop or a performance-sensitive scenario, it is recommended to use the StringBuilder class. The StringBuilder provides a more efficient way to build strings by appending individual parts.

val stringBuilder = StringBuilder()

for (i in 1..10) {
stringBuilder.append("Number: ").append(i).append("\n")
}

val result = stringBuilder.toString()
println(result)

In the above example, we first create an instance of the StringBuilder class. Inside the loop, we append the string "Number: " followed by the current value of i and a newline character. Finally, we convert the StringBuilder to a regular string using the toString() method and print the result.

Using the plusAssign Operator (+=)

Kotlin also provides the plusAssign operator (+=), which can be used to concatenate strings in a more concise way.

var str = "Hello"

str += " World"
println(str) // Output: Hello World

In the above example, we declare a variable str with the initial value "Hello". The plusAssign operator is then used to concatenate the string " World" to the existing value of str. The resulting string is stored back in str and printed to the console.

Conclusion

In this tutorial, we explored various methods to concatenate strings in Kotlin. We learned how to use the plus operator (+), string templates, StringBuilder, and the plusAssign operator (+=). By understanding these different approaches, you can choose the most suitable method based on your specific use case.