How to repeat a String in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

How to repeat a String in java

in python "#"*5 is equal to "#####" but how to do that in java is there an easy method ?

13th Nov 2016, 3:55 PM
Sunera Avinash
Sunera Avinash - avatar
6 Answers
+ 4
You could use a for loop, or just copy/paste
13th Nov 2016, 3:56 PM
Keto Z
Keto Z - avatar
+ 3
with java8 you can use this oneliner: String.join("", Collections.nCopies(5, "#"));
17th Nov 2016, 5:25 PM
0xCAFEBABE
0xCAFEBABE - avatar
+ 2
Check out my stringrepeatfunction program.
13th Nov 2016, 5:17 PM
Remmiq Sliv
Remmiq Sliv - avatar
+ 2
you could use a loop class repeat { public static void main (String[] args) { string test = "#" for(int i=0, i<5, i++) { System.out.print(test); } } }
17th Nov 2016, 8:05 PM
Graham Caswell
Graham Caswell - avatar
+ 1
oh I thought there would be an easy method anyway thanks
13th Nov 2016, 4:00 PM
Sunera Avinash
Sunera Avinash - avatar
+ 1
thanks I just thought there would be some kind of built in function
13th Nov 2016, 5:22 PM
Sunera Avinash
Sunera Avinash - avatar