Here is the shortest version (Java 1.5+ required):
repeated = new String(new char[n]).replace("\0", s);
Where n is the number of times you want to repeat the string and s is the string to repeat.
No imports or libraries needed.
Answer from user102008 on Stack OverflowHere is the shortest version (Java 1.5+ required):
repeated = new String(new char[n]).replace("\0", s);
Where n is the number of times you want to repeat the string and s is the string to repeat.
No imports or libraries needed.
If you are using Java <= 7, this is as "concise" as it gets:
// create a string made up of n copies of string s
String.format("%0" + n + "d", 0).replace("0", s);
In Java 8 and above there is a more readable way:
// create a string made up of n copies of string s
String.join("", Collections.nCopies(n, s));
Finally, for Java 11 and above, there is a new repeat(int count) method specifically for this purpose(link)
"abc".repeat(12);
Alternatively, if your project uses java libraries there are more options.
For Apache Commons:
StringUtils.repeat("abc", 12);
For Google Guava:
Strings.repeat("abc", 12);
Can I multiply strings in Java to repeat sequences? - Stack Overflow
What is the simple way to repeat a string in Java?
You could use Apache commons-lang (which has an impressive collection of handy string utilities):
StringUtils.repeat(3, "abc")
If you're using Java 8:
Collections.nCopies(3, "abc").collect(Collectors.joining("")); More on reddit.com This Minecraft Seed Makes Everything Repeat
The seeds (Note you can add multiples of 2^48 to these seeds to get more):
(1,1)
102496288339226
243233776694554
25503018638468
166240506993796
194863804823673
54126316468345
180285425070967
39547936715639
229379390903807
88641902548479
11631417851241
152368906206569
278457215087719
137719726732391
60743603216145
201481091571473
74753121498560
215490609853888
123866381128776
264603869484104
187592346697722
46854858342394
172961353594608
32223865239280
60829983200677
201567471556005
236703458319970
95965969964642
159072574510805
18335086155477
82025653894727
222763142250055
(-1,1)
74811678275130
109996050363962
145180422452794
180364794541626
215549166630458
250733538719290
4442934097466
39627306186298
225951381575532
261135753664364
14845149042540
50029521131372
85213893220204
120398265309036
155582637397868
190767009486700
43099331928968
78283704017800
113468076106632
148652448195464
183836820284296
219021192373128
254205564461960
7914959840136
194239033000162
229423405088994
264607777177826
18317172556002
53501544644834
88685916733666
123870288822498
159054660911330
0 , 0 mineshaft seed repeating along (-1,13): 52649853323471
0 , 0 mineshaft seed repeating along (8,15): 19685303093557
All Broken Seeds
More on reddit.comIs it possible to loop a jukebox on JAVA?
Videos
The easiest way in plain Java with no dependencies is the following one-liner:
new String(new char[generation]).replace("\0", "-")
Replace generation with number of repetitions, and the "-" with the string (or char) you want repeated.
All this does is create an empty string containing n number of 0x00 characters, and the built-in String#replace method does the rest.
Here's a sample to copy and paste:
public static String repeat(int count, String with) {
return new String(new char[count]).replace("\0", with);
}
public static String repeat(int count) {
return repeat(count, " ");
}
public static void main(String[] args) {
for (int n = 0; n < 10; n++) {
System.out.println(repeat(n) + " Hello");
}
for (int n = 0; n < 10; n++) {
System.out.println(repeat(n, ":-) ") + " Hello");
}
}
String::repeat
Use String::repeat in Java 11 and later.
Examples
"A".repeat( 3 )
AAA
And the example from the Question.
int i = 3; //frequency to repeat
String someNum = "123"; // initial string
String ch = "0"; // character to append
someNum = someNum + ch.repeat(i); // formulation of the string
System.out.println(someNum); // would result in output -- "123000"