Repeat a string: Difference between revisions

→‎{{header|Java}}: code cleanup
(→‎{{header|Java}}: code cleanup)
Line 898:
 
There's no method or operator to do this in Java, so you have to do it yourself.
 
<lang java5>public static String repeat(String str, int times) {
StringBuilder retsb = new StringBuilder(str.length() * times);
for (int i = 0; i < times; i++) ret.append(str);
return ret.toString();
sb.append(str);
return retsb.toString();
}
 
public static void main(String[] args) {
System.out.println(repeat("ha", 5));
}</lang>
 
Or even shorter:
 
<lang java5>public static String repeat(String str, int times) {
return new String(new char[times]).replace("\0", str);
}</lang>