Saturday, January 17, 2026
HomeLanguagesJavaString Class repeat() Method in Java with Examples

String Class repeat() Method in Java with Examples

The string can be repeated N number of times, and we can generate a new string that has repetitions. repeat() method is used to return String whose value is the concatenation of given String repeated count times. If the string is empty or the count is zero then the empty string is returned. 

Syntax:

string.repeat(count);

Parameter: Accepts an integer count which is the number of times we want to repeat the string.

Returns: String whose value is the concatenation of given String repeated count times

Example:

Input:

string = abc
count = 3

Output:

abcabcabc

Input:

string = xyz
count = 0

Output:

null

Algorithm:

  1. Firstly, we take the input of String.
  2. Then we use the repeat() method with the number of counts we want to repeat the string.
     

Example 1:

Java




// Java program to demonstrate the usage of 
// repeat() method
  
import java.io.*;
  
class GFG {
    public static void main (String[] args) {
        
      String string="abc";
        
      int count=3;
        
      System.out.println("String :"+string.repeat(count));
  
      }
}


Output

String :abcabcabc

Example 2:

Java




// Java program to demonstrate the usage of 
// repeat() method
  
import java.io.*;
  
class GFG {
    public static void main (String[] args) {
        
       String string="xyz";
       int count=0;
        
       System.out.println("string :"+string.repeat(count));
           
    }
}


Output

string :
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

1 COMMENT

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
117 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS