Java StringUtils repeat() Method

Java StringUtils repeat() Method | In this blog, we will discuss the repeat() method given in the StringUtils class. This method is used to repeat the string or the character a specified number of times.

Java StringUtils repeat() Method Syntax

Java StringUtils repeat() method has the following three variations:-

  1. public static String repeat(final char ch, final int repeat):- Returns padding using the specified delimiter repeated to a given length.
  2. public static String repeat(final String str, final int repeat):- Repeat a String given repeat times to form a new String.
  3. public static String repeat(final String str, final String separator, final int repeat):- Repeat a String given repeat times to form a new String, with a String separator injected each time.

Parameters:
ch: the character needed to be repeated.
str: the string needed to be repeated.
repeat: the number of times it should be repeated.
separator: this specifies the character to separate each string, if not mentioned then by default the string with repetition string will be printed together with no space.
Return type: String
Returns: the string after repeating.

Example of StringUtils.repeat() Java Method

Example-1:-
String: “Mango”
Number of repetitions: 2
After repetition: “MangoMango”

Example-2:-
String: “Mango”
Separator: “,”
Number of repetitions: 2
After repetition: “Mango,Mango”

Example-3:-
Character: ‘c’
Number of repetitions: 2
After repetition: “cc”

StringUtils.repeat() Java Examples

Let us see an example of the public static String repeat(final String str, final int repeat) method which is used to repeat the String a given number of times to form a new String.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String str = "Hello";
        System.out.println("String after repetition: " + StringUtils.repeat(str, 4));
        str = " ";
        System.out.println("String after repetition: " + StringUtils.repeat(str, 3));
        str = "Abc";
        System.out.println("String after repetition: " + StringUtils.repeat(str, 3));
        str = null;
        System.out.println("String after repetition: " + StringUtils.repeat(str, 3));
        str = "null";
        System.out.println("String after repetition: " + StringUtils.repeat(str, 3));
    }
}

Output:

String after repetition: HelloHelloHelloHello
String after repetition:
String after repetition: AbcAbcAbc
String after repetition: null
String after repetition: nullnullnull

Let us see an example of the public static String repeat(final String str, final String separator, final int repeat) method which is used to repeat the String a given number of times to form a new String, with a String separator injected each time.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Hello";
        System.out.println("String after repetition: " + StringUtils.repeat(string, " ", 4));
        string = "Abc";
        System.out.println("String after repetition: " + StringUtils.repeat(string, ", ", 3));
        string = null;
        System.out.println("String after repetition: " + StringUtils.repeat(string, " /", 3));

    }
}

Output:-

String after repetition: Hello Hello Hello Hello
String after repetition: Abc, Abc, Abc
String after repetition: null

How To Use StringUtils In Java To repeat A Character

Let us see an example of the public static String repeat(final char ch, final int repeat) method which returns padding using the specified delimiter repeated to a given length.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        char ch = 'B';
        System.out.println("Character after repetition: " + StringUtils.repeat(ch, 4));
        ch = 'a';
        System.out.println("Character after repetition: " + StringUtils.repeat(ch, 3));
        ch = ' ';
        System.out.println("Character after repetition: " + StringUtils.repeat(ch, 3));
    }
}

Output:

Character after repetition: BBBB
Character after repetition: aaa
Character after repetition:

String repeat Java Without StringUtils

We can also repeat a character or String without using StringUtils class. The String class itself contains the repeat() method which can repeat the string N number of times.

Java program to repeat a string without StringUtils

public class Main {
    public static void main(String[] args) {
        String string = "KnowProgram";
        System.out.println("String: " + string.repeat(2));
        string = "Abc";
        System.out.println("String: " + string.repeat(5));
    }
}

Output:-

String: KnowProgramKnowProgram
String: AbcAbcAbcAbcAbc

Java program to repeat a character without StringUtils

public class Main {
    public static void main(String[] args) {
        char ch = 'H';
        // convert char to String
        String string = String.valueOf(ch);
        System.out.println("String: " + string.repeat(2));

        ch = '9';
        System.out.println("String: " + String.valueOf(ch).repeat(5));
    }
}

Output:-

String: HH
String: 99999

Limitations with String class repeat() Method & Why do we need to use StringUtils class repeat() Method

  1. Using the String class repeat() method we can’t inject a separator in between the repeated string or character.
  2. To repeat a character, the character needs to be converted to a string.
  3. String class methods don’t handle null and when we apply the repeat() method on a null reference then it gives NullPointerException.
import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = null;
        System.out.println("Using StringUtils.repeat(): " + StringUtils.repeat(string, 2));

        System.out.println("Using String class repeat(): ");
        System.out.println(string.repeat(2));
    }
}

Output:-

Using StringUtils.repeat(): null
Using String class repeat():
Exception in thread “main” java.lang.NullPointerException
at Main.main(Main.java:9)

4. When we pass the negative value as the number of repeats to the StringUtils.repeat() method then it returns an empty string. But the String class repeat() method throws java.lang.IllegalArgumentException exception.

import org.apache.commons.lang3.StringUtils;

public class Main {
    public static void main(String[] args) {
        String string = "Hello";
        String newString = StringUtils.repeat(string, -2);
        System.out.println("Using StringUtils.repeat(): " + newString);
        System.out.println("Length: " + newString.length());

        System.out.println("Using String class repeat(): ");
        System.out.println(string.repeat(-2));
    }
}

Output:-

Using StringUtils.repeat():
Length: 0
Using String class repeat():
Exception in thread “main” java.lang.IllegalArgumentException: count is negative: -2
at java.base/java.lang.String.repeat(String.java:3149)
at Main.main(Main.java:11)

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 *