Anonymous user
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
for (int i = 0; i < times; i++)
return ret.toString();▼
sb.append(str);
}
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>
|