Repeat a string: Difference between revisions

Content added Content deleted
(Add Processing)
Line 48: Line 48:


In Flex, there is the method [http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/StringUtil.html#repeat%28%29 mx.utils.StringUtil.repeat()].
In Flex, there is the method [http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/StringUtil.html#repeat%28%29 mx.utils.StringUtil.repeat()].

===Iterative version===
===Iterative version===
<lang ActionScript>function repeatString(string:String, numTimes:uint):String
<lang ActionScript>function repeatString(string:String, numTimes:uint):String
Line 56: Line 57:
return output;
return output;
}</lang>
}</lang>

===Recursive version===
===Recursive version===
The following double-and-add method is much faster when repeating a string many times.
The following double-and-add method is much faster when repeating a string many times.
Line 65: Line 67:
return tmp + tmp;
return tmp + tmp;
}</lang>
}</lang>

===Flex===
===Flex===
<lang ActionScript>import mx.utils.StringUtil;
<lang ActionScript>import mx.utils.StringUtil;
Line 334: Line 337:


=={{header|Burlesque}}==
=={{header|Burlesque}}==

<lang burlesque>
<lang burlesque>
blsq ) 'h5?*
blsq ) 'h5?*
Line 531: Line 533:
writeln(chars);
writeln(chars);
}</lang>
}</lang>

=={{header|DCL}}==
=={{header|DCL}}==
Not exactly what the task asks for but at least it is something;
Not exactly what the task asks for but at least it is something;
Line 577: Line 580:


=={{header|Déjà Vu}}==
=={{header|Déjà Vu}}==

<lang dejavu>!. concat( rep 5 "ha" )</lang>
<lang dejavu>!. concat( rep 5 "ha" )</lang>
{{out}}
{{out}}
Line 793: Line 795:
*****
*****
</pre>
</pre>

=={{header|Free Pascal}}==
<lang pascal>strUtils.dupeString('ha', 5)</lang>
Repetition of a single character:
<lang pascal>stringOfChar('*', 5)</lang>
If the repeated character happens to be the space character:
<lang pascal>space(5)</lang>


=={{header|Frink}}==
=={{header|Frink}}==
Line 986: Line 995:
<lang javascript>
<lang javascript>
console.log("ha".repeat(5)); // hahahahaha</lang>
console.log("ha".repeat(5)); // hahahahaha</lang>

====Repetition by Egyptian multiplication====
====Repetition by Egyptian multiplication====
For larger numbers of repetitions, however, it proves significantly faster to progressively double a copy of the original string (concatenating it with itself). Intermediate stages of doubling are appended to an accumulator wherever required for binary composition of the target number.
For larger numbers of repetitions, however, it proves significantly faster to progressively double a copy of the original string (concatenating it with itself). Intermediate stages of doubling are appended to an accumulator wherever required for binary composition of the target number.
Line 1,326: Line 1,336:
}
}
}</lang>
}</lang>




=={{header|NetRexx}}==
=={{header|NetRexx}}==
Line 1,434: Line 1,442:


=={{header|Oforth}}==
=={{header|Oforth}}==

<lang Oforth>StringBuffer new "abcd" <<n(5)</lang>
<lang Oforth>StringBuffer new "abcd" <<n(5)</lang>


Line 1,477: Line 1,484:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==

===Version #1. Based on recursion.===
===Version #1. Based on recursion.===
This solution is recursive and unimaginably bad. Slightly less bad versions can be designed, but that's not the point: don't use GP for text processing if you can avoid it. If you really need to, it's easy to create an efficient function in PARI (see [[#C|C]]) and pass that to GP.
This solution is recursive and unimaginably bad. Slightly less bad versions can be designed, but that's not the point: don't use GP for text processing if you can avoid it. If you really need to, it's easy to create an efficient function in PARI (see [[#C|C]]) and pass that to GP.
Line 1,540: Line 1,546:


=={{header|Pascal}}==
=={{header|Pascal}}==
See [[#Delphi|Delphi]] or [[#Free Pascal|Free Pascal]], as standard Pascal does not know strings of unlimited length.
See [[Repeat_a_string#Delphi | Delphi]]


=={{header|Perl}}==
=={{header|Perl}}==
Line 1,675: Line 1,681:


=={{header|Racket}}==
=={{header|Racket}}==

<lang racket>
<lang racket>
#lang racket
#lang racket
Line 1,891: Line 1,896:


=={{header|Scratch}}==
=={{header|Scratch}}==

This example requires making variables named "String", "Count", and "Repeated" first.
This example requires making variables named "String", "Count", and "Repeated" first.


Line 2,337: Line 2,341:
defer allocator.free(ex);
defer allocator.free(ex);
}</lang>
}</lang>

=={{header|zkl}}==
=={{header|zkl}}==
Same as Ruby
Same as [[#Ruby|Ruby]]
<lang zkl>"ha" * 5 # --> "hahahahaha"</lang>
<lang zkl>"ha" * 5 # --> "hahahahaha"</lang>