1. Introduction

While string repetition may not be an everyday task, it becomes essential in specific scenarios. Several methods can be used to repeat a string n number of times in Kotlin.

In this tutorial, we’ll explore various methods available for repeating a String in Kotlin.

2. Using a for() Loop

First, we can quickly achieve our goal with the use of the classic for() loop:

fun repeatString(string: String, n: Int): String {
    var result = ""
    for (i in 1..n) {
        result += string
    }

    return result
}

Here, we define a method repeatString() that accepts two parameters: the String we wish to repeat and an Int denoting the number of repetitions. We then concatenate the string n times and return the result.

Now, let’s test our method for correctness:

@Test
fun `repeat string n times using for loop`() {
    val string = "Hello World"
    val n = 3

    assertEquals("Hello WorldHello WorldHello World", repeatString(string, n))
}

3. Using the String.repeat() Method

Kotlin’s String.repeat() method is a built-in method that we can use to repeat a string n number of times:

@Test
fun `repeat string n times using repeat() method`() {
    val str = "Hello World"
    val repeated = str.repeat(3)

    assertEquals("Hello WorldHello WorldHello World", repeated)
}

In this example, we use the String.repeat() method to repeat the “Hello World” string three times and store the result in a new variable, repeated.

4. Using a repeat() Loop With StringBuilder

Kotlin provides another type of loop expression we can leverage: the repeat() loop. We can combine this loop with a StringBuilder and its append() method, which we can use to concatenate strings:

@Test
fun `repeat string n times using stringbuilder method`() {
    val str = "Hello World"
    val sb = StringBuilder()

    repeat(3) {
        sb.append(str)
    }

    val repeated = sb.toString()

    assertEquals("Hello WorldHello WorldHello World", repeated)
} 

In this code, we create a new StringBuilder object and use the special repeat() lambda to repeat the string three times. We use the append() method to build up the string. Finally, we convert the StringBuilder to a String using the toString() method and store the result in a new variable, repeated.

5. Using Sequence and joinToString() Methods

The next method makes use of a Sequence and joinToString() to repeat a string n number of times:

@Test
fun `repeat string n times using Sequence and JoinToString method`() {
    val str = "Hello World"
    val repeated = generateSequence { str }.take(3).joinToString("")

    assertEquals("Hello WorldHello WorldHello World", repeated)
}

In this example, we use the generateSequence() method to create an infinite sequence of the string we passed as a parameter. Subsequently, we use the take() method to take the first n elements of the Sequence, and then call joinToString() to concatenate them into a single String.

6. Using a CharArray and the String Constructor

We can also use a CharArray and the String constructor to repeat a string n number of times:

@Test
fun `repeat string n times using CharArray and String constructor method`() {
    val str = "Hello World"
    val repeated = String(CharArray(str.length * 3) { str[it % str.length] })

    assertEquals("Hello WorldHello WorldHello World", repeated)
}

In the code above, we create a CharArray with a size denoting the length of the resulting string after repetition. In this case, we set this size to three times the original string, just enough to contain the “Hello World” string after three repetitions.

We then use an initializer function as the second parameter to the CharArray constructor. The initializer function generates values by index to fill in the CharArray. We’re able to loop over our original string in the initializer by index using the modulo (%) operator on the array’s index and the length of our original string.

Finally, we convert the CharArray to a string using the String constructor.

7. Conclusion