To append a character to the end of a string in Kotlin, you can use the plus
operator or StringBuilder
.
In this example,
str1
with the value "Hello World"
.'!'
to str1
using the plus
operator.str1
to standard output.fun main(args: Array<String>) {
var str1 = "Hello World"
str1 += '!'
println("Appended String: $str1")
}
Appended String: Hello World!
In this example,
str2
with the value "apple"
.StringBuilder
to append the character '!'
to str2
.str2
to standard output.fun main(args: Array<String>) {
var str2 = "apple"
val stringBuilder = StringBuilder(str2)
stringBuilder.append('!')
println("Appended String: $stringBuilder")
}
Appended String: apple!
In this tutorial, we learned How to Append a Character to end of a String in Kotlin language with well detailed examples.