Sobes.tech
Junior — Middle

What methods can be used to concatenate strings in Kotlin?

sobes.tech AI

Answer from AI

In Kotlin, strings can be concatenated in several ways:

  1. Using the + operator:
val result = "Hello, " + "world!"
  1. String interpolation (string templates):
val name = "John"
val greeting = "Hello, $name!"
  1. Using the plus() method:
val result = "Hello, ".plus("world!")
  1. Using StringBuilder for efficient concatenation of many strings:
val builder = StringBuilder()
builder.append("Hello, ")
builder.append("world!")
val result = builder.toString()
  1. Using the joinToString() function to concatenate collections of strings:
val words = listOf("Hello", "world")
val result = words.joinToString(separator = ", ")

The choice of method depends on the task and performance considerations.

What methods can be used to concatenate strings in… - sobes.tech