Suppose you have the string ‘qwe’ and you need to repeat string ‘n’ times. Here’s how to do it with StringBuilder:
Using StringBuilder and loop
1 2 3 4 5 6 7 8 |
int n = 3; String in = "qwe"; StringBuilder b = new StringBuilder(n * in.length()); for (int i = 0; i < n; i += 1) { b.append(in); } String newString = b.toString(); |