Repeat a string: Difference between revisions

Added Java
(new task with implementations)
 
(Added Java)
Line 1:
{{task|String manipulation}}Take a string and repeat it some number of times. Example: repeat("ha", 5) => "hahahahaha"
=={{header|Java}}==
{{works with|Java|1.5+}}
 
There's no function or operator to do this in Java, so you have to do it yourself.
Take a string and repeat it some number of times. Example: repeat("ha", 5) => "hahahahaha"
<lang java5>public static String repeat(String str, int times){
StringBuilder ret = new StringBuilder(str);
for(int i = 1;i < times;i++) ret.append(str);
return ret.toString();
}
 
public static void main(String[] args){
System.out.println(repeat("ha", 5));
}</lang>
=={{header|JavaScript}}==
This solution creates an empty array of size n+1 and then joins it using the target string as the delimiter
Anonymous user