How to concatenate two strings in Kotlin

String concatenation is essential in many programming scenarios, such as dynamically generating messages, constructing complex strings for output, or building queries and paths. It becomes especially important in cases where efficiency and readability are critical, such as in performance-sensitive applications or when dealing with user-generated content.

Concatenating strings in Kotlin can be achieved in various ways, depending on the context and what we’re trying to accomplish. To demonstrate one of the popular techniques, consider the following important points and an example.

Methods for string concatenation

  1. Using the + operator: The most straightforward way to concatenate strings is by using the + operator. This method is intuitive and works well for simple concatenations.

String concatenation
String concatenation
  1. String templates: Kotlin supports string templates, which allow us to include variables directly within a string by prefixing the variable name with a $ symbol. For more complex expressions, we can use curly braces (${expression}).

  2. The joinToString function: When we have a collection of strings that we want to concatenate—possibly with a delimiter, prefix, or postfix—the joinToString function is very handy.

  3. The StringBuilder: For more complex or performance-critical scenarios, such as concatenating strings within a loop, using a StringBuilder can be more efficient. It avoids creating many intermediate string objects.

Code example

Let’s say we want to greet a user by concatenating a greeting with the user’s name.

fun main() {
val greeting = "Hello"
val name = "Alice"
// Using the + operator
val message1 = greeting + ", " + name + "!"
// Using string templates
val message2 = "$greeting, $name!"
// Using StringBuilder
val stringBuilder = StringBuilder()
stringBuilder.append(greeting).append(", ").append(name).append("!")
val message3 = stringBuilder.toString()
// Using joinToString function
val words = listOf(greeting, name)
val message4 = words.joinToString(separator = ", ", postfix = "!")
// Print results
println(message1) // Output: Hello, Alice!
println(message2) // Output: Hello, Alice!
println(message3) // Output: Hello, Alice!
println(message4) // Output: Hello, Alice!
}

Explanation

  • Lines 2–3: Two variables, greeting and name, are initialized with the string values "Hello" and "Alice", respectively.

  • Line 6: Concatenates the strings using the + operator to form message1.

  • Line 9: Utilizes string templates to embed variables directly in the string, forming message2.

  • Lines 12–14: Uses a StringBuilder object to efficiently build a string by appending each component separately, then converts it to a string to form message3.

  • Lines 17–18: Uses the joinToString function to concatenate elements of a list into a single string with a specified separator and postfix, forming message4.

  • Lines 20–24: Outputs the four concatenated messages to the console.

Conclusion

Kotlin’s approach to string concatenation is both efficient and developer-friendly, offering various methods to suit different needs.

From simple concatenations to more complex expressions embedded within strings, Kotlin ensures code readability and maintainability. Understanding and utilizing these string manipulation techniques can significantly enhance our coding efficiency and capability in Kotlin.

Embracing these features allows for expressive and concise code, making string manipulation tasks simpler and more intuitive.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved