How to repeat string n times in java?
Suppose you have the string ‘qwe’ and you need to repeat string ‘n’ times. Here’s how to do it: Using java 8 stream api
1 2 3 4 5 |
String newString = Collections.nCopies(3, "qwe").stream().collect(Collectors.joining("")); String newString = Stream.generate(() -> "qwe").limit(3).collect(Collectors.joining()); String newString = String.join("", Collections.nCopies(n, s)); |
Using String.format
1 2 3 4 |
String s = "qwe"; int n = 3; //this line print 'qweqweqwe' String newString = String.format("%0" + n + "d", 0).replace("0", s); |
Let us examine …
Read More