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:
- Firstly, we take the input of String.
- Then we use the repeat() method with the number of counts we want to repeat the string.
Example 1:
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
String string= "abc" ;
int count= 3 ;
System.out.println( "String :" +string.repeat(count));
}
}
|
Example 2:
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
String string= "xyz" ;
int count= 0 ;
System.out.println( "string :" +string.repeat(count));
}
}
|
Last Updated :
28 Jan, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...