Kotlin – Repeat String N Times

Repeat a String N Times in Kotlin

To repeat a given string for n times in Kotlin, you can use String.repeat() function.

Call repeat() function on the given String object str, and pass the integer value of n as argument.

str.repeat(n)

repeat() function creates and returns a new string by repeating the given string for n times.

Examples

Repeat “apple” for five times

In the following program, we take a string str and repeat this string for five times.

Kotlin Program

fun main() {
    val str = "apple"
    val n = 5
    val resultString = str.repeat(n)
    println(resultString)
}

Output

appleappleappleappleapple