## The challenge

Write a function called `repeat_str` which repeats the given string `src` exactly `count` times.

 ``````1 2 `````` ``````repeatStr(6, "I") // "IIIIII" repeatStr(5, "Hello") // "HelloHelloHelloHelloHello" ``````

## The solution in Java

Java comes with a `built-in` on the `String` class. Called `repeat`, which allows you to very easily repeat that string `n` number of times.

 ``````1 `````` ``````"Hello".repeat(2); // "HelloHello" ``````

### Using String::repeat()

Therefore, we could solve our above problem as follows:

 ``````1 2 3 4 5 `````` ``````class Solution { static String repeatStr(int repeat, String string) { return string.repeat(repeat); } } ``````

Obviously, we probably want to validate our input before we just do it, so maybe wrap it in a ternary:

 ``````1 2 3 4 5 `````` ``````public class Solution { public static String repeatStr(final int repeat, final String string) { return repeat >= 0 ? string.repeat(repeat) : ""; } } ``````

### Write all the code yourself?

Alternatively, you could always just write the method yourself, doing something like this:

 `````` 1 2 3 4 5 6 7 8 9 10 11 `````` ``````public class Solution { public static String repeatStr(final int repeat, final String string) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < repeat; i++) { sb.append(string); } return sb.toString(); } } ``````

Or maybe with less code, like this:

 ``````1 2 3 4 5 6 7 `````` ``````public class Solution { public static String repeatStr(final int repeat, final String string) { String s = ""; for (int i = 0; i < repeat; i++) s += string; return s; } } ``````

### Java 8 and above

If you’re feeling Java8 and above thoughts, you could always do this:

 ``````1 2 3 4 5 6 `````` ``````public class Solution { public static String repeatStr(final int repeat, final String string) { return java.util.stream.IntStream.range(0, repeat).mapToObj(i -> string) .collect(java.util.stream.Collectors.joining()).toString(); } } ``````

Or maybe combine it with the `Collections` `nCopies` possible solution:

 ``````1 2 3 4 5 6 7 `````` ``````import java.util.Collections; public class Solution { public static String repeatStr(final int repeat, final String string) { return repeat < 0 ? "" : String.join("", Collections.nCopies(repeat, string)); } } ``````

## Test cases to validate

It’s always important to have some tests which will help us validate our application code.

 `````` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 `````` ``````import org.junit.Test; import static org.junit.Assert.assertEquals; import java.util.Random; public class SolutionTest { @Test public void test4a() { assertEquals("aaaa", Solution.repeatStr(4, "a")); } @Test public void test3Hello() { assertEquals("HelloHelloHello", Solution.repeatStr(3, "Hello")); } @Test public void test5empty() { assertEquals("", Solution.repeatStr(5, "")); } @Test public void test0a() { assertEquals("", Solution.repeatStr(0, "kata")); } private final char[] characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQERSTUVWXYZ0123456789!@#\$%^&*()-=_+[]{}|;:,.<>/?`~".toCharArray(); @Test public void testRandom() { final Random rand = new Random(); for (int testIteration = 0; testIteration < 10; ++testIteration) { final StringBuilder text = new StringBuilder(rand.nextInt(32)); for (int i = 0; i < text.capacity(); ++i) { final int chi = rand.nextInt(characters.length); text.append(characters[chi]); } final String string = text.toString(); final int timesToRepeat = rand.nextInt(32); final StringBuilder sb = new StringBuilder(timesToRepeat * string.length()); for (int i = 0; i < timesToRepeat; ++i) { sb.append(text); } final String expected = sb.toString(); assertEquals(expected, Solution.repeatStr(timesToRepeat, string)); } } } ``````