This article explores different ways to concatenate integers to a string in Kotlin.

1. Using String templates

An elegant and concise solution to concatenate multiple integers to a string is using String templates in Kotlin. A template expression starts with a dollar sign ($) and consists of a name or an expression:

Download Code

2. Using String concat (+)

Another option is to use the String concat operator + to concatenate integers to a string. This is demonstrated below using an empty string literal (or valueOf() function):

Download Code

3. Using joinToString() function

To concatenate multiple integers to a string, it is useful to create a utility function that accepts a vararg. The joinToString() function can join all vararg elements using a separator and transform function (to convert each integer to a string).

Download Code

4. Using String.format() function

Finally, we can use the String.format() function to get the formatted string from the specified integer arguments with the %s format specifier.

Download Code

That’s all about concatenating integers to a string in Kotlin.