Java String repeat() Method

Java String repeat() Method | In this blog, we will discuss how to repeat a string in Java. By using this repeat() method we can repeat the string N number of times. The new string can be generated with repetitions. The repeat() method takes a parameter that is count, meaning the repeat method returns the concatenation of the given string by count times. If the string is empty then the method returns an empty string.

The syntax for the Java Repeat String:- public String repeat(int count)
Parameters: count:- number of times the string needs to be repeated.
Return type:- string
Returns:- the string with repetition.
If this string is empty or the count is zero then the empty string is returned.

Example for String Repeat Java
Example-1:-
String = “Hi”
Count = 2
Output string = “HiHi”

Example-2:-
String = “LOL”
Count = 4
Output string = “LOLLOLLOLLOL”

Java String repeat() Method Example

public class Main {
   public static void main(String[] args) {
      String str = "Java";
      int count = 4;
      System.out.println("String: " + str.repeat(count));
   }
}

Output:-

String: JavaJavaJavaJava

We have initialized a string variable with a string literal “Java”. Later we called the repeat() method of Java string class by passing count value 4. Therefore it returns “JavaJavaJavaJava”.

Program to Demonstrate How To Repeat A String In Java

public class Main {
   public static void main(String[] args) {
      String str = "Abc";
      int count = 0;
      String str1 = str.repeat(count);
      System.out.println("String: " + str1);
      System.out.println("Length: " + str1.length());
   }
}

Output:-

String:
Length: 0

Since the count value is 0 therefore Java string repeat() method returns an empty string. Similarly, if the input string is empty in that case also repeat() method always returns an empty string. Below example demonstrating it:-

public class Main {
   public static void main(String[] args) {
      String str = "";

      String str1 = str.repeat(5);
      System.out.println("String1: " + str1);
      System.out.println("Length: " + str1.length());

      String str2 = str.repeat(0);
      System.out.println("String2: " + str2);
      System.out.println("Length: " + str2.length());
   }
}

Output:-

String1:
Length: 0
String2:
Length: 0

Exceptions Thrown by Java String.repeat() Method

The Java string repeat() method throws IllegalArgumentException and OutOfMemoryError. It throws IllegalArgumentException if the count value is negative. And it gives OutOfMemoryError when (Integer.MAX_VALUE / count < string.length). Let us see it through an example:-

public class Main {
   public static void main(String[] args) {
      String str = "Hello";

      String str1 = str.repeat(-5);
      System.out.println("String1: " + str1);
      System.out.println("Length: " + str1.length());
   }
}

Output:-

Exception in thread “main” java.lang.IllegalArgumentException: count is negative: -5

public class Main {
   public static void main(String[] args) {
      String str = "Hello";

      String str1 = str.repeat(Integer.MAX_VALUE);
      System.out.println("String1: " + str1);
      System.out.println("Length: " + str1.length());
   }
}

Output:-

Exception in thread “main” java.lang.OutOfMemoryError: Required length exceeds implementation limit

How to Repeat a String N Times in Java

To repeat any string in Java N number of times we can use the Java string class repeat() method. The value of N must be positive. If the N value is negative then we will get IllegalArgumentException and if the N value is zero then we will get an empty string.

public class Main {
   public static void main(String[] args) {
      String string = "Apple";
      String repeatedString = string.repeat(2);
      System.out.println(repeatedString);
   }
}

Output:-

AppleApple

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.