Following are the different ways by which we can concatenate two strings in Kotlin:
Using String Interpolation : We use the technique of string interpolation to concatenate the two strings. Basically, we substitute the strings in place of their placeholders in the initialisation of the third string.
val s1 = "FreeTime"
val s2 = "Learning"
val s3 = "$s1 $s2" // stores "FreeTime Learning"​
Using the + or plus() operator : We use the ‘
+
’ operator to concatenate the two strings and store them in a third variable.
val s1 = "FreeTime"
val s2 = "Learning"
val s3 = s1 + s2 // stores "FreeTimeLearning"
val s4 = s1.plus(s2) // stores "FreeTimeLearning"​
Using StringBuilder : We concatenate two strings using the StringBuilder object. First, we append the first string and then the second string.
val s1 = "FreeTime"
val s2 = "Learning"
val s3 = StringBuilder()
s3.append(s1).append(s2)
val s4 = s3.toString() // stores "FreeTimeLearning"​