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 the 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 that case also the 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

If we want to overcome some of these limitations then we can use the StringUtils class repeat() method. There we have explained the limitations of the String class repeat() Method & why we need to use the StringUtils class repeat() Method.

Like other string class methods String class repeat() method also doesn’t handle null, and when we call the repeat() method on null reference then it gives NullPointerException. The StringUtils class repeat() method also gives the option to inject some separator in between the repeated string or characters. See more:- StringUtils class repeat() method

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";
      int number = 2;
      String repeatedString = null;
      if(string != null && number >= 0 && number <= Integer.MAX_VALUE) {
          repeatedString = string.repeat(number);
      }
      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. Required fields are marked *