How to correctly concatenate a String in Kotlin?

Technology CommunityCategory: KotlinHow to correctly concatenate a String in Kotlin?
VietMX Staff asked 4 years ago

In Kotlin, you can concatenate 1. using string interpolation / templates

val a = "Hello"
val b = "World"
val c = "$a $b"
  1. using the + / plus() operator
    val a = "Hello"
    val b = "World" 
    val c = a + b   // same as calling operator function a.plus(b)
    val c = a.plus(b)
    
    print(c)
  2. using the StringBuilder
    val a = "Hello"
    val b = "World"
    
    val sb = StringBuilder()
    sb.append(a).append(b)
    val c = sb.toString()
    
    print(c)