How to repeat a string n times in Java
Learn, how to repeat a string n times in Java.
Repeating a string
In Java 11, we have a built-in String.repeat() method by using that we can repeat a string n times.
Here is an example, that repeats the string 'fox' 5 times.
public class Main
{
public static void main(String[] args)
{
String str = "fox";
String repeatedVal = str.repeat(5);
System.out.println(repeatedVal);
}
}Output:
foxfoxfoxfoxfox

