
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String repeat() Method
The Java String repeat() method concatenates a string with itself the specified number of times and returns the result. The given string can be repeated n number of times, and since the string is Java is immutable a new string is created and the result of the above operation is stored in it.
If the count value is zero or the string is empty, this method an empty string. Arrays.fill() and System.arraycopy()methods are internally called by this method to create a new string.
Syntax
Following is the syntax for Java String repeat() method −
public String repeat(int count)
Parameters
count − It is the number of times the string is to be repeated.
Return Value
This method returns a new string whose value is the concatenation of this String repeated count of times. If the count is zero or the string is empty, the empty string is returned.
Example
The following example shows the usage of Java String repeat() method. Here we are declaring a String object 'str' with the value "Java" and trying to repeat it 5 times. −
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "Java"; int count = 5; System.out.println("The new repeated string is: " + str.repeat(count)); } }
Output
If you compile and run the above program, it will produce the following result −
The new repeated string is: JavaJavaJavaJavaJava
Example
If you pass 0 as an argument to this method or the current string is empty, this method returns an empty string.
In the code below two String objects 'str' and 'str1' are created with values "Program" and an empty string respectively. Then we are calling the repeat() method on these strings by the passing 0 as an argument.
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "Program"; String str1 = ""; int Givencount = 0; System.out.println("The new repeated string is: " + str.repeat(Givencount)); System.out.println("The new repeated string is: " + str1.repeat(Givencount)); } }
Output
If you compile and run the program above, the output will be displayed as follows −
The new repeated string is: The new repeated string is:
Example
If you pass '1' as an argument to this method, it returns the current string with out any changes.
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "Program"; int Givencount = 1; System.out.println("The new repeated string is: " + str.repeat(Givencount)); } }
Output
On executing the program above, the output is obtained as follows −
The new repeated string is: Program
Example
If you pass a negative value as an argument to this method it throws an IllegalArgumentException.
import java.lang.*; public class StringDemo { public static void main(String[] args) { String str = "Program"; int Givencount = -12; System.out.println("The new repeated string is: " + str.repeat(Givencount)); } }
Output
The output of the above program is as follows −
Exception in thread "main" java.lang.IllegalArgumentException: count is negative: -12 at java.base/java.lang.String.repeat(String.java:4410) at StringDemo.main(StringDemo.java:7)