The string.repeat() function in Java works in such a manner that it returns a new string to the number of times it has been specified as input. If the value entered is null, the string.repeat() function will return empty. A negative input will result in an error. Since the string can not be repeated in negative.
In addition to string.repeat() method, there are many other methods present to repeat a string such as nCopies() and replace(). A simple and quick implementation is using the string.repeat() to repeat strings in Java.
This article will discuss the implementation of string.repeat() function in Java.
How to Use/Implement String.repeat() in Java?
There are different methods to repeat a string in Java, but the repeat() method is effective and easy to implement. The input declared is repeated for a specified number of times. The algorithm to repeat the string is depicted below:
- Enter the input.
- Use string.repeat(count) for the number of times the string has to be repeated.
Example 1: If the Input Is Declared As a Value
In the following code, a simple string will be declared and is used with the repeat() function to repeat the given string:
class repeatstring {
public static void main(String[] args){//Pass a string
String str = "Hello";//Mention the values to repeat
String newStr = str.repeat(3);
System.out.println("The String is " +newStr);
}
}
In the code above
- A string named “str” is declared.
- A repeat() function function is used with the input string using the dot syntax.
- A value is declared in str.repeat() to print the result at the specified times.
Output

The output is declared three times in concatenated form.
Example 2: If the Input Is Declared as 0
If the count in the repeat.string() function is declared as 0, the results will be empty.
class repeatstring {
public static void main(String[] args){
//Enter the string
String str = "Hello";
//If the number of time needed to repeat the string is 0
String newStr = str.repeat(0);
//The output will appear empty
System.out.println("The String is " +newStr);
}
}
The code is the same as the previous one except that the value entered is declared as 0, returning the results as no output.
Output

The output appears to be empty if the entered value is “0”
Example 3: If the Input Is a Negative Number
If the input value is declared as negative, then an IllegalArgumentException will be generated on the screen.
//Declare a class and input the string
class repeatstring {
public static void main(String[] args){
String str = "Hello";
String newStr = str.repeat(-1);
System.out.println(newStr);
}
}
Output
The below output indicates that str.repeat function does not work for negative values.

Conclusion
The string.repeat() function in Java works in such a manner that it returns a new string to the number of times it has been specified as input. For some conditions like that of 0, the screen goes empty without any output and for negative input, an exception occurs that is IllegalArgumentException.This article has exhibited the implementation of String.repeat() in Java along with different conditions in a simple manner.