In this section, we will learn what the String repeat() method is and how to use it in Java.
What is Java String repeat() Method?
The `repeat()` method is used to repeat the value of a string a number of times and return it as a new string object.
Note: this method does not change the original string value.
Java repeat() Method Syntax:
public String repeat(int count)
repeat() Method Parameters:
This method takes only one argument, and that is the number of times we want to repeat the content of the target string value.
repeat() Method Return Value:
The return value of this method is a new string value with the content of the target string repeated multiple times (equal to the number we’ve set as the argument to the method).
repeat() Method Exceptions:
The method does not throw an exception.
Example: using String repeat() method
public class Simple {
public static void main(String[] args) {
String hi = "Hello";
System.out.println(hi.repeat(3));
}
}
Output:
HelloHelloHello