To append a string to another string in Kotlin, you can use the plus
operator or the plusAssign
operator to concatenate the strings.
In this example,
str1
with a value of "Hello"
.plus
operator to append the string " World"
to str1
.plus
operator concatenates the two strings and returns the result.fun main() {
val str1 = "Hello"
val concatenated = str1 + " World"
println(concatenated)
}
Hello World
In this example,
str2
with a value of "banana"
.plusAssign
operator to append the string "apple"
to str2
.plusAssign
operator concatenates the two strings and assigns the result to the variable str2
.fun main() {
var str2 = "banana"
str2 += "apple"
println(str2)
}
bananaapple
In this tutorial, we learned How to Append a String to another String in Kotlin language with well detailed examples.