This article explores different ways to append a character at the end of a String in Kotlin. Since strings are immutable in Kotlin, the solution creates a new String instance.

1. Using plus() function

A simple solution to append characters to the end of a String is using the + (or +=) operator.

Download Code

 
Alternatively, we can use the plus() function to concatenate a char with the given string.

Download Code

2. Using String templates

Another concise solution to append a character to a string is with String templates. A template expression starts with a dollar sign ($) and consists of a name or an expression:

Download Code

3. Using String builder

The idea here is to convert the string to a String builder and call its append() function to append a character. This logic would translate to the following code:

Download Code

4. Using String.format() function

Finally, we can use the String.format() function to concatenate a character with a string. This is demonstrated below:

Download Code

That’s all about appending a character to the end of a String in Kotlin.