I don't believe Java natively provides this feature, although it would be nice. I write Perl code occasionally and the x operator in Perl comes in really handy for repeating strings!
However StringUtils in commons-langprovides this feature. The method is called repeat(). Your only other option is to build it manually using a loop.
If you're repeating single characters like the OP, and the maximum number of repeats is not too high, then you could use a simple substring operation like this:
int i = 3;
String someNum = "123";
someNum += "00000000000000000000".substring(0, i);
private String repeatString(String s,int count){
StringBuilder r = new StringBuilder();
for (int i = 0; i < count; i++) {
r.append(s);
}
return r.toString();
}
A generalisation of Dave Hartnoll's answer (I am mainly taking the concept ad absurdum, maybe don't use that in anything where you need speed).
This allows one to fill the String up with i characters following a given pattern.
int i = 3;
String someNum = "123";
String pattern = "789";
someNum += "00000000000000000000".replaceAll("0",pattern).substring(0, i);
If you don't need a pattern but just any single character you can use that (it's a tad faster):
int i = 3;
String someNum = "123";
char c = "7";
someNum += "00000000000000000000".replaceAll("0",c).substring(0, i);
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"