Repeat a string: Difference between revisions

m (→‎{{header|68000 Assembly}}: added actual string to print)
 
(25 intermediate revisions by 21 users not shown)
Line 12:
 
=={{header|11l}}==
<syntaxhighlight lang ="11l">print(‘ha’ * 5)</langsyntaxhighlight>
 
{{out}}
Line 20:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Repeat a string - 19/04/2020
REPEATS CSECT
USING REPEATS,R13 base register
Line 62:
PG DC CL80' ' pg
REGEQU
END REPEATS </langsyntaxhighlight>
{{out}}
<pre>
Line 70:
 
=={{header|4DOS Batch}}==
<langsyntaxhighlight lang="4dos">gosub repeat ha 5
echo %@repeat[*,5]
quit
Line 79:
enddo
echo.
return</langsyntaxhighlight>
Output shows:
<pre>hahahahaha
Line 85:
 
=={{header|6502 Assembly}}==
<langsyntaxhighlight lang="6502asm">CHROUT equ $FFD2 ;KERNAL call, prints the accumulator to the screen as an ascii value.
 
org $0801
Line 127:
TestStr:
db "HA",0</langsyntaxhighlight>
{{out}}
<pre>
Line 143:
=={{header|68000 Assembly}}==
Easiest way to do this is with a loop.
<langsyntaxhighlight lang="68000devpac">MOVE.W #5-1,D1
RepString:
LEA A3, MyString
Line 153:
 
MyString:
DC.B "ha",2550
even</langsyntaxhighlight>
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="asm8080"> org 100h
jmp demo
 
; Repeat the string at DE into HL, B times
repeat: mvi c,'$' ; string terminator
xra a ; repeat 0x?
ora b
mov m,c ; then empty string
rz
rpt1: push d ; save begin of string to repeat
chcpy: ldax d ; copy character from input to output
mov m,a
inx d ; advance pointers
inx h
cmp c ; end of string?
jnz chcpy
pop d ; restore begin of string to repeat
dcx h ; move back past terminator in copy
dcr b ; done yet?
jnz rpt1 ; if not add another copy
ret
 
demo: lxi d,ha ; get string to repeat
lxi h,buf ; place to store result
mvi b,5 ; repeat 5 times
call repeat
lxi d,buf ; print result using CP/M call
mvi c,9
jmp 5
 
ha: db 'ha$' ; string to repeat
buf: ds 32 ; place to store repeated string</syntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
=={{header|8th}}==
<langsyntaxhighlight lang="forth">"ha" 5 s:*
. cr</langsyntaxhighlight>
Output shows:
<pre>hahahahaha</pre>
Line 165 ⟶ 200:
This works for ABAP Version 7.40 and above
 
<syntaxhighlight lang="abap">
<lang ABAP>
report z_repeat_string.
 
write repeat( val = `ha` occ = 5 ).
</syntaxhighlight>
</lang>
 
{{out}}
Line 177 ⟶ 212:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">Proc Main()
byte REPEAT
 
Line 187 ⟶ 222:
Do
 
Return</langsyntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
Line 197 ⟶ 232:
 
===Iterative version===
<langsyntaxhighlight ActionScriptlang="actionscript">function repeatString(string:String, numTimes:uint):String
{
var output:String = "";
Line 203 ⟶ 238:
output += string;
return output;
}</langsyntaxhighlight>
 
===Recursive version===
The following double-and-add method is much faster when repeating a string many times.
<langsyntaxhighlight ActionScriptlang="actionscript">function repeatRecursive(string:String, numTimes:uint):String
{
if(numTimes == 0) return "";
Line 213 ⟶ 248:
var tmp:String = repeatRecursive(string, numTimes/2);
return tmp + tmp;
}</langsyntaxhighlight>
 
===Flex===
<langsyntaxhighlight ActionScriptlang="actionscript">import mx.utils.StringUtil;
trace(StringUtil.repeat("ha", 5));
</syntaxhighlight>
</lang>
Sample Output:
<pre>
Line 226 ⟶ 261:
=={{header|Ada}}==
In [[Ada]] multiplication of an universal integer to string gives the desired result. Here is an example of use:
<langsyntaxhighlight Adalang="ada">with Ada.Strings.Fixed; use Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
 
Line 232 ⟶ 267:
begin
Put_Line (5 * "ha");
end String_Multiplication;</langsyntaxhighlight>
Sample output:
<pre>
Line 239 ⟶ 274:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">call_n(5, o_text, "ha");</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">print (5 * "ha")
</syntaxhighlight>
</lang>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
<lang Amazing Hopper>
#!/usr/bin/hopper
#include <hopper.h>
Line 254 ⟶ 289:
{"ha",5}replicate, println
{0}return
</syntaxhighlight>
</lang>
hahahahaha
hahahahaha
Line 260 ⟶ 295:
=={{header|APL}}==
Fill up a string of length 10 with 'ha':
<langsyntaxhighlight lang="apl"> 10⍴'ha'
hahahahaha</langsyntaxhighlight>
Alternatively, define a function:
<langsyntaxhighlight lang="apl"> REPEAT←{(⍺×⍴⍵)⍴⍵}
5 REPEAT 'ha'
hahahahaha</langsyntaxhighlight>
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">set str to "ha"
set final_string to ""
repeat 5 times
set final_string to final_string & str
end repeat</langsyntaxhighlight>
 
 
Line 280 ⟶ 315:
 
{{trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">replicate(5000, "ha")
 
-- Repetition by 'Egyptian multiplication' -
Line 299 ⟶ 334:
end repeat
return out & dbl
end replicate</langsyntaxhighlight>
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight ApplesoftBasiclang="applesoftbasic">FOR I = 1 TO 5 : S$ = S$ + "HA" : NEXT
 
? "X" SPC(20) "X"</langsyntaxhighlight>
Output:
<pre>X X</pre>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">print repeat "ha" 5</langsyntaxhighlight>
 
{{out}}
Line 316 ⟶ 351:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % Repeat("ha",5)
 
Repeat(String,Times)
Line 323 ⟶ 358:
Output .= String
Return Output
}</langsyntaxhighlight>
 
=={{header|AutoIt}}==
<langsyntaxhighlight AutoItlang="autoit">#include <String.au3>
 
ConsoleWrite(_StringRepeat("ha", 5) & @CRLF)</langsyntaxhighlight>
 
=={{header|AWK}}==
<langsyntaxhighlight lang="awk">function repeat( str, n, rep, i )
{
for( ; i<n; i++ )
Line 340 ⟶ 375:
BEGIN {
print repeat( "ha", 5 )
}</langsyntaxhighlight>
 
=={{header|Babel}}==
<langsyntaxhighlight lang="babel">main: { "ha" 5 print_repeat }
 
print_repeat!: { <- { dup << } -> times }</langsyntaxhighlight>
Outputs:
<syntaxhighlight lang ="babel">hahahahaha</langsyntaxhighlight>
The '<<' operator prints, 'dup' duplicates the top-of-stack, 'times' does something x number of times. The arrows mean down (<-) and up (->) respectively - it would require a lengthy description to explain what this means, refer to the doc/babel_ref.txt file in the github repo linked from [[Babel]]
 
=={{header|BaCon}}==
To repeat a string:
<langsyntaxhighlight lang="qbasic">DOTIMES 5
s$ = s$ & "ha"
DONE
PRINT s$</langsyntaxhighlight>
{{out}}
<pre>
Line 361 ⟶ 396:
</pre>
To repeat one single character:
<langsyntaxhighlight lang="qbasic">PRINT FILL$(5, ASC("x"))</langsyntaxhighlight>
{{out}}
<pre>
Line 370 ⟶ 405:
=={{header|BASIC}}==
==={{header|BASIC256}}===
<langsyntaxhighlight BASIC256lang="basic256">function StringRepeat$ (s$, n)
cad$ = ""
for i = 1 to n
Line 381 ⟶ 416:
print StringRepeat$("ha", 5)
print StringRepeat$("*", 5)
end</langsyntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
<syntaxhighlight lang="qbasic">100 cls
110 print stringrepeat$("rosetta",1)
120 print stringrepeat$("ha",5)
130 print stringrepeat$("*",5)
140 end
150 function stringrepeat$(s$,n)
160 cad$ = ""
170 for i = 1 to n
180 cad$ = cad$+s$
190 next i
200 stringrepeat$ = cad$
210 end function</syntaxhighlight>
 
==={{header|GW-BASIC}}===
{{works with|Applesoft BASIC}}
{{works with|PC-BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|QBasic}}
{{works with|MSX BASIC}}
<syntaxhighlight lang="qbasic">100 CLS : rem 100 HOME for Applesoft BASIC
110 S$ = "rosetta" : N = 1
120 GOSUB 210
130 PRINT CAD$
140 S$ = "ha" : N = 5
150 GOSUB 210
160 PRINT CAD$
170 S$ = "*" : N = 5
180 GOSUB 210
190 PRINT CAD$
200 END
210 REM FUNCTION STRINGREPEAT$(S$,N)
220 CAD$ = ""
230 FOR I = 1 TO N
240 CAD$ = CAD$+S$
250 NEXT I
260 RETURN</syntaxhighlight>
 
==={{header|Minimal BASIC}}===
{{works with|Quite BASIC}}
<syntaxhighlight lang="qbasic">100 REM Repeat a string
110 LET S$ = "rosetta"
120 LET N = 1
130 GOSUB 210
140 LET S$ = "ha"
150 LET N = 5
160 GOSUB 210
170 LET S$ = "*"
180 LET N = 5
190 GOSUB 210
200 STOP
210 REM FUNCTION StringRepeat$(S$,N)
220 FOR I = 1 TO N
230 PRINT S$;
240 NEXT I
250 PRINT
260 RETURN
270 END</syntaxhighlight>
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
The [[#GW-BASIC|GW-BASIC]] solution works without any changes.
 
==={{header|QBasic}}===
<langsyntaxhighlight QBasiclang="qbasic">FUNCTION StringRepeat$ (s$, n)
cad$ = ""
FOR i = 1 TO n
Line 395 ⟶ 495:
PRINT StringRepeat$("ha", 5)
PRINT StringRepeat$("*", 5)
END</langsyntaxhighlight>
 
==={{header|Quite BASIC}}===
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|MSX BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 LET S$ = "rosetta"
115 LET N = 1
120 GOSUB 210
130 PRINT C$
140 LET S$ = "ha"
145 LET N = 5
150 GOSUB 210
160 PRINT C$
170 LET S$ = "*"
175 LET N = 5
180 GOSUB 210
190 PRINT C$
200 END
210 REM FUNCTION STRINGREPEAT$(S$,N)
220 LET C$ = ""
230 FOR I = 1 TO N
240 LET C$ = C$ + S$
250 NEXT I
260 RETURN</syntaxhighlight>
 
==={{header|True BASIC}}===
<langsyntaxhighlight lang="qbasic">FUNCTION StringRepeat$ (s$, n)
LET cad$ = ""
FOR i = 1 TO n
Line 409 ⟶ 537:
PRINT StringRepeat$("ha", 5)
PRINT StringRepeat$("*", 5)
END</langsyntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Repeat a string"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
DECLARE FUNCTION StringRepeat$ (s$, n)
 
FUNCTION Entry ()
 
PRINT StringRepeat$ ("rosetta", 1)
PRINT StringRepeat$ ("ha", 5)
PRINT StringRepeat$ ("*", 5)
 
END FUNCTION
FUNCTION StringRepeat$ (s$, n)
cad$ = ""
FOR i = 1 TO n
cad$ = cad$ + s$
NEXT i
RETURN cad$
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
<langsyntaxhighlight lang="yabasic">sub StringRepeat$ (s$, n)
cad$ = ""
for i = 1 to n
Line 423 ⟶ 575:
print StringRepeat$("ha", 5)
print StringRepeat$("*", 5)
end</langsyntaxhighlight>
 
 
=={{header|Batch File}}==
Commandline implementation
<langsyntaxhighlight lang="dos">@echo off
if "%2" equ "" goto fail
setlocal enabledelayedexpansion
Line 435 ⟶ 587:
for /l %%i in (1,1,%num%) do set res=!res!%char%
echo %res%
:fail</langsyntaxhighlight>
 
'Function' version
<langsyntaxhighlight lang="dos">@echo off
set /p a=Enter string to repeat :
set /p b=Enter how many times to repeat :
Line 447 ⟶ 599:
set "c=%c%+=1"
if /i _"%c%"==_"%d%" (exit /b)
goto :a</langsyntaxhighlight>
 
'Function' version 2
<langsyntaxhighlight lang="dos">@echo off
@FOR /L %%i in (0,1,9) DO @CALL :REPEAT %%i
@echo That's it!
Line 462 ⟶ 614:
@GOTO:EOF
 
:END</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> PRINT STRING$(5, "ha")</langsyntaxhighlight>
 
=={{header|beeswax}}==
<langsyntaxhighlight lang="beeswax"> p <
p0~1<}~< d@<
_VT@1~>yg~9PKd@M'd;</langsyntaxhighlight>
 
 
Line 482 ⟶ 634:
<code>s</code> tells the user that the program expects a string as input.
<code>i</code> tells the user that the program expects an integer as input.
 
 
=={{header|Beef}}==
<syntaxhighlight lang="csharp">
String s = new String('X', 5);
s.Replace("X", "ha");
</syntaxhighlight>
 
And for single character repeats
 
<syntaxhighlight lang="csharp">
String s1 = scope .();
s1.PadLeft(5, '*');
</syntaxhighlight>
 
 
=={{header|Befunge}}==
<langsyntaxhighlight Befungelang="befunge">v> ">:#,_v
>29*+00p>~:"0"- #v_v $
v ^p0p00:-1g00< $ >
v p00&p0-1g00+4*65< >00g1-:00p#^_@</langsyntaxhighlight>
Input sample:
<pre>ha05</pre>
Line 499 ⟶ 666:
<code>⥊</code>(reshape) can all by itself be used to repeat a string to a particular length. This function is just a wrapper around it to repeat n times.
 
<langsyntaxhighlight lang="bqn">Repeat ← ×⟜≠ ⥊ ⊢
 
•Show 5 Repeat "Hello"</langsyntaxhighlight><syntaxhighlight lang="text">"HelloHelloHelloHelloHello"</langsyntaxhighlight>
 
=={{header|Bracmat}}==
The code almost explains itself. The repetions are accumulated in a list <code>rep</code>. The <code>str</code> concatenates all elements into a single string, ignoring the white spaces separating the elements.
 
<langsyntaxhighlight lang="bracmat">(repeat=
string N rep
. !arg:(?string.?N)
Line 513 ⟶ 680:
' (!N+-1:>0:?N&!string !rep:?rep)
& str$!rep
);</langsyntaxhighlight>
 
<pre> repeat$(ha.5)
Line 520 ⟶ 687:
=={{header|Brainf***}}==
Prints "ha" 10 times. Note that this method only works for a number of repetitions that fit into the cell size.
<langsyntaxhighlight lang="bf">+++++ +++++ init first as 10 counter
[-> +++++ +++++<] we add 10 to second each loopround
 
Line 528 ⟶ 695:
 
and a newline because I'm kind and it looks good
+++++ +++++ +++ . --- .</langsyntaxhighlight>
 
=={{header|Brat}}==
<langsyntaxhighlight lang="brat">p "ha" * 5 #Prints "hahahahaha"</langsyntaxhighlight>
 
=={{header|Burlesque}}==
<langsyntaxhighlight lang="burlesque">
blsq ) 'h5?*
"hhhhh"
blsq ) "ha"5.*\[
"hahahahaha"
</syntaxhighlight>
</lang>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 563 ⟶ 730:
free(result);
return 0;
}</langsyntaxhighlight>
A variation.
<langsyntaxhighlight lang="c">...
char *string_repeat(const char *str, int n)
{
Line 577 ⟶ 744:
while (pa>=dest) *pa-- = *pb--;
return dest;
}</langsyntaxhighlight>
 
To repeat a single character
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 596 ⟶ 763:
free(result);
return 0;
}</langsyntaxhighlight>
 
If you use [[GLib]], simply use <code>g_strnfill ( gsize length, gchar fill_char )</code> function.
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">string s = "".PadLeft(5, 'X').Replace("X", "ha");</langsyntaxhighlight>
or (with .NET 2+)
<langsyntaxhighlight lang="csharp">string s = new String('X', 5).Replace("X", "ha");</langsyntaxhighlight>
or (with .NET 2+)
<langsyntaxhighlight lang="csharp">string s = String.Join("ha", new string[5 + 1]);</langsyntaxhighlight>
or (with .NET 4+)
<langsyntaxhighlight lang="csharp">string s = String.Concat(Enumerable.Repeat("ha", 5));</langsyntaxhighlight>
 
To repeat a single character:
<langsyntaxhighlight lang="csharp">string s = "".PadLeft(5, '*');</langsyntaxhighlight>
or (with .NET 2+)
<langsyntaxhighlight lang="csharp">string s = new String('*', 5);</langsyntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <string>
#include <iostream>
 
Line 629 ⟶ 796:
std::cout << repeat( "Ha" , 5 ) << std::endl ;
return 0 ;
}</langsyntaxhighlight>
 
To repeat a single character:
<langsyntaxhighlight lang="cpp">#include <string>
#include <iostream>
 
Line 638 ⟶ 805:
std::cout << std::string( 5, '*' ) << std::endl ;
return 0 ;
}</langsyntaxhighlight>
 
=== recursive version ===
<langsyntaxhighlight lang="cpp">#include <string>
#include <iostream>
Line 656 ⟶ 823:
std::cout << repeat( "Ha" , 5 ) << std::endl ;
return 0 ;
}</langsyntaxhighlight>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void repeatAString() {
print("ha".repeat(5));
}</langsyntaxhighlight>
 
=={{header|Clipper}}==
Also works with Harbour Project compiler Harbour 3.0.0 (Rev. 16951)
<langsyntaxhighlight lang="visualfoxpro"> Replicate( "Ha", 5 )</langsyntaxhighlight>
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(apply str (repeat 5 "ha"))</langsyntaxhighlight>
 
=={{header|COBOL}}==
Virtually a one-liner.
<langsyntaxhighlight lang="cobol">IDENTIFICATION DIVISION.
PROGRAM-ID. REPEAT-PROGRAM.
DATA DIVISION.
Line 680 ⟶ 847:
MOVE ALL 'ha' TO HAHA.
DISPLAY HAHA.
STOP RUN.</langsyntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
 
=={{header|ColdFusion}}==
<langsyntaxhighlight lang="cfm">
<cfset word = 'ha'>
<Cfset n = 5>
Line 691 ⟶ 858:
<Cfloop from="1" to="#n#" index="i">#word#</Cfloop>
</Cfoutput>
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defun repeat-string (n string)
(with-output-to-string (stream)
(loop repeat n do (write-string string stream))))</langsyntaxhighlight>
 
A version which allocates the result string in one step:
 
<langsyntaxhighlight lang="lisp">(defun repeat-string (n string
&aux
(len (length string))
Line 708 ⟶ 875:
for i from 0 by len
do (setf (subseq result i (+ i len)) string))
result)</langsyntaxhighlight>
 
 
For those who love one-liners, even at the expense of readability:
<langsyntaxhighlight lang="lisp">(defun repeat-string (n string)
(format nil "~V@{~a~:*~}" n string))</langsyntaxhighlight>
 
 
<langsyntaxhighlight lang="lisp">(princ (repeat-string 5 "hi"))</langsyntaxhighlight>
 
 
A single character may be repeated using just the builtin <code>make-string</code>:
<langsyntaxhighlight lang="lisp">(make-string 5 :initial-element #\X)</langsyntaxhighlight>
produces “XXXXX”.
 
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">
<lang Ruby>
puts "ha" * 5
</syntaxhighlight>
</lang>
 
<pre>hahahahaha</pre>
Line 732 ⟶ 899:
=={{header|D}}==
Repeating a string:
<langsyntaxhighlight lang="d">import std.stdio, std.array;
 
void main() {
writeln("ha".replicate(5));
}</langsyntaxhighlight>
Repeating a character with vector operations:
<langsyntaxhighlight lang="d">import std.stdio;
 
void main() {
Line 745 ⟶ 912:
chars[] = '*'; // set all characters in the string to '*'
writeln(chars);
}</langsyntaxhighlight>
 
=={{header|DCL}}==
Not exactly what the task asks for but at least it is something;
<langsyntaxhighlight DCLlang="dcl">$ write sys$output f$fao( "!AS!-!AS!-!AS!-!AS!-!AS", "ha" )
$ write sys$output f$fao( "!12*d" )</langsyntaxhighlight>
{{out}}
<pre>$ @repeat_a_string_and_then_character
Line 758 ⟶ 925:
=={{header|Delphi}}==
Repeat a string
<syntaxhighlight lang="delphi">
<lang Delphi>
function RepeatString(const s: string; count: cardinal): string;
var
Line 768 ⟶ 935:
 
Writeln(RepeatString('ha',5));
</syntaxhighlight>
</lang>
 
Repeat a character
 
<syntaxhighlight lang="delphi">
<lang Delphi>
Writeln( StringOfChar('a',5) );
</syntaxhighlight>
</lang>
 
Using recursion
 
<syntaxhighlight lang="delphi">
<lang Delphi>
function RepeatStr(const s: string; i: Cardinal): string;
begin
Line 786 ⟶ 953:
result := s + RepeatStr(s, i-1)
end;
</syntaxhighlight>
</lang>
 
Built in RTL function:
 
<syntaxhighlight lang Delphi="delphi">StrUtils.DupeString</langsyntaxhighlight>
 
=={{header|DWScript}}==
Repeat a string
 
<syntaxhighlight lang="delphi">
<lang Delphi>
PrintLn( StringOfString('abc',5) );
</syntaxhighlight>
</lang>
 
Repeat a character
 
<syntaxhighlight lang="delphi">
<lang Delphi>
PrintLn( StringOfChar('a',5) );
</syntaxhighlight>
</lang>
 
=={{header|Dyalect}}==
<langsyntaxhighlight lang="dyalect">String.Repeat("ha", 5)</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">!. concat( rep 5 "ha" )</langsyntaxhighlight>
{{out}}
<pre>"hahahahaha"</pre>
 
=={{header|E}}==
<langsyntaxhighlight lang="e">"ha" * 5</langsyntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
func$ rep s$ n .
for i to n
r$ &= s$
.
return r$
.
print rep "ha" 5
</syntaxhighlight>
 
=={{header|ECL}}==
After version 4.2.2
<syntaxhighlight lang="text">IMPORT STD; //Imports the Standard Library
STRING MyBaseString := 'abc';
RepeatedString := STD.Str.Repeat(MyBaseString,3);
RepeatedString; //returns 'abcabcabc'</langsyntaxhighlight>
 
Before version 4.2.2
<syntaxhighlight lang="text">RepeatString(STRING InStr, INTEGER Cnt) := FUNCTION
rec := {STRING Str};
ds := DATASET(Cnt,TRANSFORM(rec,SELF.Str := InStr));
Line 833 ⟶ 1,011:
 
RepeatString('ha',3);
RepeatString('Who',2);</langsyntaxhighlight>
 
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
String funny = "ha" * 5;
String stars = '*' * 80;
</syntaxhighlight>
 
=={{header|Egison}}==
<langsyntaxhighlight lang="egison">
(S.concat (take 5 (repeat1 "ha")))
</syntaxhighlight>
</lang>
 
=={{header|Eiffel}}==
<langsyntaxhighlight lang="eiffel">
repeat_string(a_string: STRING; times: INTEGER): STRING
require
Line 848 ⟶ 1,032:
Result := a_string.multiply(times)
end
</syntaxhighlight>
</lang>
 
=={{header|Elena}}==
ELENA 46.x :
<langsyntaxhighlight lang="elena">import system'routines;
import extensions;
import extensions'text;
Line 858 ⟶ 1,042:
public program()
{
var s := new Range(0, 5).selectBy::(x => "ha").summarize(new StringWriter())
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">
String.duplicate("ha", 5)
</syntaxhighlight>
</lang>
 
=={{header|Emacs Lisp}}==
Going via a list to repeat the desired string:
 
<langsyntaxhighlight lang="lisp">(apply 'concat (make-list 5 "ha"))</langsyntaxhighlight>
 
A single character can be repeated with <code>make-string</code>:
 
<syntaxhighlight lang ="lisp">(make-string 5 ?x)</langsyntaxhighlight>
 
The <code>cl-loop</code> macro can repeat and concatenate:
 
{{libheader|cl-lib}}
<langsyntaxhighlight lang="lisp">(require 'cl-lib)
(cl-loop repeat 5 concat "ha")</langsyntaxhighlight>
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">repeat(X,N) ->
lists:flatten(lists:duplicate(N,X)).</langsyntaxhighlight>
This will duplicate a string or character N times to produce a new string.
 
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
<lang ERRE>
PROCEDURE REPEAT_STRING(S$,N%->REP$)
LOCAL I%
Line 895 ⟶ 1,079:
END FOR
END PROCEDURE
</syntaxhighlight>
</lang>
Note: If N% is less than 1, the result is the empty string "".If S$ is a one-character string
you can use the predefined function <code>STRING$</code> as <code>REP$=STRING$(S$,N%)</code>.
Line 902 ⟶ 1,086:
=={{header|Euphoria}}==
A simple loop will do:
<syntaxhighlight lang="euphoria">
<lang Euphoria>
sequence s = ""
for i = 1 to 5 do s &= "ha" end for
Line 908 ⟶ 1,092:
 
hahahahaha
</syntaxhighlight>
</lang>
 
For repeating a single character:
<syntaxhighlight lang="euphoria">
<lang Euphoria>
sequence s = repeat('*',5)
 
*****
</syntaxhighlight>
</lang>
 
For repeating a string or sequence of numbers:
<syntaxhighlight lang="euphoria">
<lang Euphoria>
include std/console.e -- for display
include std/sequence.e -- for repeat_pattern
Line 928 ⟶ 1,112:
hahahahaha
{1,2,3,1,2,3,1,2,3,1,2,3,1,2,3}
</syntaxhighlight>
</lang>
 
But wait, here's another way:
<syntaxhighlight lang="euphoria">
<lang Euphoria>
include std/console.e -- for display
include std/sequence.e -- for flatten
sequence s = flatten(repeat("ha",5))
display(s)
</syntaxhighlight>
</lang>
 
note: repeat creates a sequence of ha's as shown below; flatten concatenates them.
<syntaxhighlight lang="euphoria">
<lang Euphoria>
{
"ha",
Line 947 ⟶ 1,131:
"ha"
}
</syntaxhighlight>
</lang>
 
=={{header|Explore}}==
Line 955 ⟶ 1,139:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">> String.replicate 5 "ha";;
val it : string = "hahahahaha"</langsyntaxhighlight>
Or
<langsyntaxhighlight lang="fsharp">> String.Concat( Array.create 5 "ha" );;
val it : string = "hahahahaha"</langsyntaxhighlight>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">: repeat-string ( str n -- str' ) swap <repetition> concat ;
 
"ha" 5 repeat-string print</langsyntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">: place-n { src len dest n -- }
0 dest c!
n 0 ?do src len dest +place loop ;
 
s" ha" pad 5 place-n
pad count type \ hahahahaha</langsyntaxhighlight>
The same code without the use of locals:
<langsyntaxhighlight lang="forth">
: place-n ( src len dest n -- )
swap >r 0 r@ c!
Line 981 ⟶ 1,165:
 
s" ha" pad 5 place-n
pad count type \ hahahahaha</langsyntaxhighlight>
Filling a string with a single character is supported by ANS-Forth:
<langsyntaxhighlight lang="forth">pad 10 char * fill \ repeat a single character
pad 10 type \ **********</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with |Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">program test_repeat
 
write (*, '(a)') repeat ('ha', 5)
 
end program test_repeat</langsyntaxhighlight>
Output:
hahahahaha
 
=={{header|Free Pascal}}==
<langsyntaxhighlight lang="pascal">strUtils.dupeString('ha', 5)</langsyntaxhighlight>
Repetition of a single character:
<syntaxhighlight lang ="pascal">stringOfChar('*', 5)</langsyntaxhighlight>
If the repeated character happens to be the space character:
<syntaxhighlight lang ="pascal">space(5)</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
' A character is essentially a string of length 1 in FB though there is a built-in function, String,
Line 1,032 ⟶ 1,216:
Print
Print "Press any key to quit program"
Sleep</langsyntaxhighlight>
 
{{out}}
Line 1,042 ⟶ 1,226:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
println[repeat["ha", 5]]
</syntaxhighlight>
</lang>
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
CFStringRef repeatStr
repeatStr = @"Hello, World!\n"
NSLog( @"%@", fn StringByPaddingToLength( @"", 50 * len(repeatStr), repeatStr, 0 ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
[50 line repetitions of "Hello, World!"]
</pre>
 
 
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=bdd2a7a0f4f09ff67e9d78b5d9667bdb Click this link to run this code]'''
<syntaxhighlight lang="text">Public Sub Main()
 
Print String$(5, "ha")
 
End</langsyntaxhighlight>
 
Output = hahahahaha
 
=={{header|GAP}}==
<langsyntaxhighlight lang="gap">Concatenation(ListWithIdenticalEntries(10, "BOB "));
"BOB BOB BOB BOB BOB BOB BOB BOB BOB BOB "</langsyntaxhighlight>
 
=={{header|Glee}}==
<syntaxhighlight lang ="glee">'*' %% 5</langsyntaxhighlight>
 
<langsyntaxhighlight lang="glee">'ha' => Str;
Str# => Len;
1..Len %% (Len * 5) => Idx;
Str [Idx] $;</langsyntaxhighlight>
 
<langsyntaxhighlight lang="glee">'ha'=>S[1..(S#)%%(S# *5)]</langsyntaxhighlight>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">fmt.Println(strings.Repeat("ha", 5)) // ==> "hahahahaha"</langsyntaxhighlight>
There is no special way to repeat a single character, other than to convert the character to a string. The following works:
<langsyntaxhighlight lang="go">fmt.Println(strings.Repeat(string('h'), 5)) // prints hhhhh</langsyntaxhighlight>
 
=={{header|Groovy}}==
<syntaxhighlight lang ="groovy"> println 'ha' * 5</langsyntaxhighlight>
 
=={{header|Harbour}}==
<langsyntaxhighlight lang="visualfoxpro">? Replicate( "Ha", 5 )</langsyntaxhighlight>
 
=={{header|Haskell}}==
For a string of finite length:
<langsyntaxhighlight lang="haskell">concat $ replicate 5 "ha"</langsyntaxhighlight>
 
Or with list-monad (a bit obscure):
<langsyntaxhighlight lang="haskell">[1..5] >> "ha"</langsyntaxhighlight>
 
Or with Control.Applicative:
<langsyntaxhighlight lang="haskell">[1..5] *> "ha"</langsyntaxhighlight>
 
For an infinitely long string:
<langsyntaxhighlight lang="haskell">cycle "ha"</langsyntaxhighlight>
 
To repeat a single character:
<syntaxhighlight lang ="haskell">replicate 5 '*'</langsyntaxhighlight>
 
Or, unpacking the mechanism of '''replicate''' a little, and using a '''mappend'''-based rep in lieu of the '''cons'''-based '''repeat''', so that we can skip a subsequent '''concat''':
<langsyntaxhighlight lang="haskell">repString :: String -> Int -> String
repString s n =
let rep x = xs
Line 1,106 ⟶ 1,308:
 
main :: IO ()
main = print $ repString "ha" 5</langsyntaxhighlight>
{{Out}}
<pre>"hahahahaha"</pre>
Line 1,112 ⟶ 1,314:
As the number of repetitions grows, however, it may become more efficient to repeat by progressive duplication (mappend to self), mappending to an accumulator only where required for binary composition of the target length. (i.e. Rhind Papyrus 'Egyptian' or 'Ethiopian' multiplication):
 
<langsyntaxhighlight lang="haskell">import Data.Tuple (swap)
import Data.List (unfoldr)
import Control.Monad (join)
Line 1,136 ⟶ 1,338:
-- TEST -----------------------------------------------------------------------
main :: IO ()
main = print $ repString 500 "ha"</langsyntaxhighlight>
 
=={{header|HicEst}}==
<langsyntaxhighlight HicEstlang="hicest">CHARACTER out*20
 
EDIT(Text=out, Insert="ha", DO=5)</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
The procedure <tt>repl</tt> is a supplied function in Icon and Unicon.
<langsyntaxhighlight Iconlang="icon">procedure main(args)
write(repl(integer(!args) | 5))
end</langsyntaxhighlight>
If it weren't, one way to write it is:
<langsyntaxhighlight Iconlang="icon">procedure repl(s, n)
every (ns := "") ||:= |s\(0 <= n)
return ns
end</langsyntaxhighlight>
 
=={{header|Idris}}==
<langsyntaxhighlight Idrislang="idris">strRepeat : Nat -> String -> String
strRepeat Z s = ""
strRepeat (S n) s = s ++ strRepeat n s
Line 1,161 ⟶ 1,363:
chrRepeat : Nat -> Char -> String
chrRepeat Z c = ""
chrRepeat (S n) c = strCons c $ chrRepeat n c</langsyntaxhighlight>
 
=={{header|Inform 7}}==
<langsyntaxhighlight lang="inform7">Home is a room.
 
To decide which indexed text is (T - indexed text) repeated (N - number) times:
Line 1,174 ⟶ 1,376:
When play begins:
say "ha" repeated 5 times;
end the story.</langsyntaxhighlight>
 
=={{Header|Insitux}}==
 
<syntaxhighlight lang="insitux">
(str* "ha" 5)
</syntaxhighlight>
 
{{out}}
 
<pre>
hahahahaha
</pre>
 
=={{header|IS-BASIC}}==
<langsyntaxhighlight ISlang="is-BASICbasic"> 10 PRINT STRING$("ha",5)
100 DEF STRING$(S$,N)
105 LET ST$=""
Line 1,184 ⟶ 1,398:
130 NEXT
140 LET STRING$=ST$
150 END DEF</langsyntaxhighlight>
 
=={{header|J}}==
<langsyntaxhighlight lang="j"> 5 # '*' NB. repeat each item 5 times
*****
5 # 'ha' NB. repeat each item 5 times
Line 1,193 ⟶ 1,407:
5 ((* #) $ ]) 'ha' NB. repeat array 5 times
hahahahaha
5 ;@# < 'ha' NB. using boxing is used to treat the array as a whole
hahahahaha</langsyntaxhighlight>
 
=={{header|Java}}==
There are a few ways to achieve this in Java.<br />
Starting with Java 11 you can use the ''String.repeat'' method.
<syntaxhighlight lang="java">
"ha".repeat(5);
</syntaxhighlight>
Which, if you view its implementation, is just using the ''Arrays.fill'' method.
<syntaxhighlight lang="java">
String[] strings = new String[5];
Arrays.fill(strings, "ha");
StringBuilder repeated = new StringBuilder();
for (String string : strings)
repeated.append(string);
</syntaxhighlight>
And if you look at the 'Arrays.fill' implementation, it's just a for-loop, which is likely the most idiomatic approach.
<syntaxhighlight lang="java">
String string = "ha";
StringBuilder repeated = new StringBuilder();
int count = 5;
while (count-- > 0)
repeated.append(string);
</syntaxhighlight>
<br />
Or
{{works with|Java|1.5+}}
 
There'sBefore Java 11 there was no method or operator to do this in Java, so you havehad to do it yourself.
 
<langsyntaxhighlight lang="java5">public static String repeat(String str, int times) {
StringBuilder sb = new StringBuilder(str.length() * times);
for (int i = 0; i < times; i++)
Line 1,210 ⟶ 1,447:
public static void main(String[] args) {
System.out.println(repeat("ha", 5));
}</langsyntaxhighlight>
 
Or even shorter:
 
<langsyntaxhighlight lang="java5">public static String repeat(String str, int times) {
return new String(new char[times]).replace("\0", str);
}</langsyntaxhighlight>
 
In Apache Commons Lang, there is a [http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/StringUtils.html#repeat%28java.lang.String,%20int%29 StringUtils.repeat()] method.
Line 1,223 ⟶ 1,460:
====Extending the String prototype====
This solution creates an empty array of length n+1, then uses the array's join method to effectively concatenate the string n times. Note that extending the prototype of built-in objects is not a good idea if the code is to run in a shared workspace.
<langsyntaxhighlight lang="javascript">String.prototype.repeat = function(n) {
return new Array(1 + (n || 0)).join(this);
}
 
console.log("ha".repeat(5)); // hahahahaha</langsyntaxhighlight>
 
As of ES6, `repeat` is built in, so this can be written as:
 
<langsyntaxhighlight lang="javascript">
console.log("ha".repeat(5)); // hahahahaha</langsyntaxhighlight>
 
====Repetition by Egyptian multiplication====
Line 1,239 ⟶ 1,476:
See the technique of 'Egyptian Multiplication' described in the Rhind Mathematical Papyrus at the British Museum.
 
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,257 ⟶ 1,494:
 
return replicate(5000, "ha")
})();</langsyntaxhighlight>
 
====Concat . replicate====
Or, more generically, we could derive '''repeat''' as the composition of '''concat''' and '''replicate'''
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,287 ⟶ 1,524:
// TEST -------------------------------------------------------------------
return repeat(5, 'ha');
})();</langsyntaxhighlight>
{{Out}}
<pre>hahahahaha</pre>
 
=={{header|jqJoy}}==
<syntaxhighlight lang="joy">DEFINE repeat == "" rotate [concat] cons times.
<lang jq>"a " * 3' # => "a a a "</lang>
 
"ha" 5 repeat.</syntaxhighlight>
Note that if the integer multiplicand is 0, then the result is null.
 
=={{header|jq}}==
<syntaxhighlight lang="jq">"a " * 3 # => "a a a "</syntaxhighlight>
Note that if the integer multiplicand is 0, then the result is the JSON value '''null'''.
 
=={{header|Julia}}==
{{works with|Julia|1.0}}
 
<langsyntaxhighlight lang="julia">@show "ha" ^ 5
 
# The ^ operator is really just call to the `repeat` function
@show repeat("ha", 5)</langsyntaxhighlight>
 
=={{header|K}}==
 
<syntaxhighlight lang="k">
<lang k>
,/5#,"ha"
"hahahahaha"
Line 1,312 ⟶ 1,553:
5#"*"
"*****"
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">fun main(args: Array<String>) {
println("ha".repeat(5))
}</langsyntaxhighlight>
Or more fancy:
<langsyntaxhighlight lang="scala">operator fun String.times(n: Int) = this.repeat(n)
 
fun main(args: Array<String>) = println("ha" * 5)</langsyntaxhighlight>
 
=={{header|LabVIEW}}==
I don't know if there is a built-in function for this, but it is easily achieved with a For loop and Concatenate Strings.<br/>
[[file:LabVIEW_Repeat_a_string.png]]
<br/>
By using built in functions:
 
[[File:Panel.png]]
[[File:BlockDiagram.png]]
<br/>
 
=={{header|Lambdatalk}}==
<langsyntaxhighlight lang="scheme">
{S.map {lambda {_} ha} {S.serie 1 10}}
-> ha ha ha ha ha ha ha ha ha ha
Line 1,351 ⟶ 1,598:
{repeat ha 10}
-> hahahahahahahahahahaha
</syntaxhighlight>
</lang>
 
=={{header|Lang}}==
<syntaxhighlight lang="lang">
# Repeat text function
fn.println(fn.repeatText(5, ha))
# Output: hahahahaha
 
# Mul operator
fn.println(parser.op(ha * 5))
# Output: hahahahaha
 
# Mul operator function
fn.println(fn.mul(ha, 5))
# Output: hahahahaha
</syntaxhighlight>
 
=={{header|langur}}==
<syntaxhighlight lang="langur">"ha" * 5</syntaxhighlight>
This example looks like Perl, but the x operator doesn't just multiply strings in langur.
<lang langur>"ha" x 5</lang>
 
=={{header|Lasso}}==
<syntaxhighlight lang Lasso="lasso">'ha'*5 // hahahahaha</langsyntaxhighlight>
 
<langsyntaxhighlight Lassolang="lasso">loop(5) => {^ 'ha' ^} // hahahahaha</langsyntaxhighlight>
 
=={{header|LFE}}==
<langsyntaxhighlight lang="lisp">
(string:copies '"ha" 5)
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
<langsyntaxhighlight lang="lb">a$ ="ha "
print StringRepeat$( a$, 5)
 
Line 1,379 ⟶ 1,640:
next i
StringRepeat$ =o$
end function</langsyntaxhighlight>
 
=={{header|Lingo}}==
*Take a string and repeat it some number of times.
<langsyntaxhighlight lang="lingo">on rep (str, n)
res = ""
repeat with i = 1 to n
Line 1,389 ⟶ 1,650:
end repeat
return res
end</langsyntaxhighlight>
<langsyntaxhighlight lang="lingo">put rep("ha", 5)
-- "hahahahaha"</langsyntaxhighlight>
*If there is a simpler/more efficient way to repeat a single “character”...
<langsyntaxhighlight lang="lingo">put bytearray(5, chartonum("*")).readRawString(5)
-- "*****"</langsyntaxhighlight>
 
=={{header|LiveCode}}==
<langsyntaxhighlight liveCodelang="livecode">on mouseUp
put repeatString("ha", 5)
end mouseUp
Line 1,406 ⟶ 1,667:
end repeat
return t
end repeatString</langsyntaxhighlight>
 
=={{header|Logo}}==
<langsyntaxhighlight lang="logo">to copies :n :thing [:acc "||]
if :n = 0 [output :acc]
output (copies :n-1 :thing combine :acc :thing)
end</langsyntaxhighlight>
or using cascade:
<langsyntaxhighlight lang="logo">show cascade 5 [combine "ha ?] "|| ; hahahahaha</langsyntaxhighlight>
 
Lhogho doesn't have cascade (yet), nor does it have the initialise a missing parameter capability demonstrated by the [:acc "||] above.
 
<langsyntaxhighlight lang="logo">to copies :n :thing :acc
if :n = 0 [output :acc]
output (copies :n-1 :thing combine :acc :thing)
end
 
print copies 5 "ha "||</langsyntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">function repeats(s, n) return n > 0 and s .. repeats(s, n-1) or "" end</langsyntaxhighlight>
 
Or use native string library function
<syntaxhighlight lang ="lua">string.rep(s,n)</langsyntaxhighlight>
 
=={{header|Maple}}==
There are many ways to do this in Maple. First, the "right" (most efficient) way is to use the supplied procedures for this purpose.
<syntaxhighlight lang="maple">
<lang Maple>
> use StringTools in
> Repeat( "abc", 10 ); # repeat an arbitrary string
Line 1,441 ⟶ 1,702:
 
"xxxxxxxxxxxxxxxxxxxx"
</syntaxhighlight>
</lang>
These next two are essentially the same, but are less efficient (though still linear) because they create a sequence of 10 strings before concatenating them (with the built-in procedure cat) to form the result.
<syntaxhighlight lang="maple">
<lang Maple>
> cat( "abc" $ 10 );
"abcabcabcabcabcabcabcabcabcabc"
Line 1,449 ⟶ 1,710:
> cat( seq( "abc", i = 1 .. 10 ) );
"abcabcabcabcabcabcabcabcabcabc"
</syntaxhighlight>
</lang>
You ''can'' build up a string in a loop, but this is highly inefficient (quadratic); don't do this.
<syntaxhighlight lang="maple">
<lang Maple>
> s := "":
> to 10 do s := cat( s, "abc" ) end: s;
"abcabcabcabcabcabcabcabcabcabc"
</syntaxhighlight>
</lang>
If you need to build up a string incrementally, use a StringBuffer object, which keeps things linear.
 
Line 1,461 ⟶ 1,722:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">StringRepeat["ha", 5]</syntaxhighlight>
<lang Mathematica>(* solution 1 *)
rep[n_Integer,s_String]:=Apply[StringJoin,ConstantArray[s,{n}]]
(* solution 2 -- @@ is the infix form of Apply[] *)
rep[n_Integer,s_String]:=StringJoin@@Table[s,{n}]
(* solution 3 -- demonstrating another of the large number of looping constructs available *)
rep[n_Integer,s_String]:=Nest[StringJoin[s, #] &,s,n-1]</lang>
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">function S = repeat(s , n)
S = repmat(s , [1,n]) ;
return</langsyntaxhighlight>
 
Note 1: The repetition is returned, not displayed.
Line 1,478 ⟶ 1,734:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">"$*"(s, n) := apply(sconcat, makelist(s, n))$
infix("$*")$
 
"abc" $* 5;
/* "abcabcabcabcabc" */</langsyntaxhighlight>
 
=={{header|Mercury}}==
Mercury's 'string' module provides an efficient char-repeater. The following uses string.builder to repeat strings.
 
<langsyntaxhighlight Mercurylang="mercury">:- module repeat.
:- interface.
:- import_module string, char, int.
Line 1,512 ⟶ 1,768:
print(Stream, String, !S),
printn(Stream, N - 1, String, !S)
; true ).</langsyntaxhighlight>
 
=={{header|min}}==
{{works with|min|0.19.3}}
<langsyntaxhighlight lang="min">"ha" 5 repeat print</langsyntaxhighlight>
{{out}}
<pre>
Line 1,523 ⟶ 1,779:
 
=={{header|MiniScript}}==
<langsyntaxhighlight MiniScriptlang="miniscript">str = "Lol"
print str * 5</langsyntaxhighlight>
{{out}}
<pre>
Line 1,531 ⟶ 1,787:
 
=={{header|Mirah}}==
<langsyntaxhighlight lang="mirah">x = StringBuilder.new
 
5.times do
Line 1,537 ⟶ 1,793:
end
 
puts x # ==> "hahahahaha"</langsyntaxhighlight>
 
=={{header|Monte}}==
<syntaxhighlight lang="monte">
<lang Monte>
var s := "ha " * 5
traceln(s)
</syntaxhighlight>
</lang>
 
=={{header|MontiLang}}==
<syntaxhighlight lang MontiLang="montilang">|ha| 5 * PRINT .</langsyntaxhighlight>
Or with a loop
<langsyntaxhighlight MontiLanglang="montilang">FOR 5
|ha| OUT .
ENDFOR || PRINT .</langsyntaxhighlight>
 
Or ...
 
<langsyntaxhighlight MontiLanglang="montilang">|ha| FOR 5 OUT ENDFOR . || PRINT .</langsyntaxhighlight>
 
=={{header|MUMPS}}==
<langsyntaxhighlight MUMPSlang="mumps">RPTSTR(S,N)
;Repeat a string S for N times
NEW I
Line 1,566 ⟶ 1,822:
F I=1:1:N W S
Q
</syntaxhighlight>
</lang>
 
 
This last example uses the [http://docs.intersystems.com/cache20121/csp/docbook/DocBook.UI.Page.cls?KEY=RCOS_fpiece#RCOS_B57001 $PIECE] function.
<syntaxhighlight lang="mumps">
<lang MUMPS>
;Even better (more terse)
S x="",$P(x,"-",10)="-"
W x
</syntaxhighlight>
</lang>
 
=={{header|Nanoquery}}==
In Nanoquery, multiplying strings by an integer returns a new string with the original value repeated.
<langsyntaxhighlight Nanoquerylang="nanoquery">"ha" * 5</langsyntaxhighlight>
 
=={{header|Neko}}==
<langsyntaxhighlight lang="actionscript">/* Repeat a string, in Neko */
var srep = function(s, n) {
var str = ""
Line 1,591 ⟶ 1,847:
}
 
$print(srep("ha", 5), "\n")</langsyntaxhighlight>
 
{{out}}
Line 1,600 ⟶ 1,856:
=={{header|Nemerle}}==
Any of the methods shown in the [[Repeat_a_string#C.23|C#]] solution would also work for Nemerle, but they're all semantically awkward. This example uses an extension method to wrap one of the awkward techniques in order to clarify the semantics (which is also possible in C#, there's nothing really Nemerle specific here except the syntax).
<langsyntaxhighlight Nemerlelang="nemerle">using System;
using System.Console;
 
Line 1,617 ⟶ 1,873:
 
}
}</langsyntaxhighlight>
 
=={{header|NetRexx}}==
NetRexx has built in functions to manipulate strings. The most appropriate for this task is the <code>'''copies()'''</code> function:
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
 
ha5 = 'ha'.copies(5)
</syntaxhighlight>
</lang>
 
There are several other built-in functions that can be used to achieve the same result depending on need:
 
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
sampleStr = 'ha' -- string to duplicate
say ' COPIES:' sampleStr.copies(5)
Line 1,640 ⟶ 1,896:
say ' SUBSTR:' ''.substr(1, 5, sampleChr)
say 'TRANSLATE:' '.....'.translate(sampleChr, '.')
</syntaxhighlight>
</lang>
 
=={{header|NewLISP}}==
<langsyntaxhighlight NewLISPlang="newlisp">(dup "ha" 5)</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import strutils
 
# Repeat a char.
Line 1,652 ⟶ 1,908:
 
# Repeat a string.
echo repeat("ha", 5) # -> "hahahahaha".</langsyntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class Repeat {
function : Main(args : String[]) ~ Nil {
Line 1,670 ⟶ 1,926:
}
}
}</langsyntaxhighlight>
 
=={{header|Objective-C}}==
Line 1,679 ⟶ 1,935:
We will extend NSString, the de facto Objective-C string class in environments that are either compatible with or descend directly from the OPENSTEP specification, such as GNUstep and Mac OS X, respectively, with a method that accomplishes the described task.
 
<langsyntaxhighlight lang="objc">@interface NSString (RosettaCodeAddition)
- (NSString *) repeatStringByNumberOfTimes: (NSUInteger) times;
@end
Line 1,687 ⟶ 1,943:
return [@"" stringByPaddingToLength:[self length]*times withString:self startingAtIndex:0];
}
@end</langsyntaxhighlight>
 
Now, let's put it to use:
<langsyntaxhighlight lang="objc"> // Instantiate an NSString by sending an NSString literal our new
// -repeatByNumberOfTimes: selector.
NSString *aString = [@"ha" repeatStringByNumberOfTimes:5];
 
// Display the NSString.
NSLog(@"%@", aString);</langsyntaxhighlight>
 
=={{header|OCaml}}==
Since Ocaml 4.02 strings are immutable, as is convenient for a functional language. Mutable strings are now implemented in the module Bytes.
<langsyntaxhighlight lang="ocaml">let string_repeat s n =
let s = Bytes.of_string s in
let len = Bytes.length s in
Line 1,707 ⟶ 1,963:
done;
(Bytes.to_string res)
;;</langsyntaxhighlight>
which gives the signature<langsyntaxhighlight lang="ocaml">val string_repeat : string -> int -> string = <fun></langsyntaxhighlight>
 
testing in the toplevel:
<langsyntaxhighlight lang="ocaml"># string_repeat "Hiuoa" 3 ;;
- : string = "HiuoaHiuoaHiuoa"</langsyntaxhighlight>
 
Alternately create an array initialized to s, and concat:
<langsyntaxhighlight lang="ocaml">let string_repeat s n =
String.concat "" (Array.to_list (Array.make n s))
;;</langsyntaxhighlight>
 
Or:
<langsyntaxhighlight lang="ocaml">let string_repeat s n =
Array.fold_left (^) "" (Array.make n s)
;;</langsyntaxhighlight>
 
To repeat a single character use:
<syntaxhighlight lang ="ocaml">String.make 5 '*'</langsyntaxhighlight>
 
=={{header|Oforth}}==
<langsyntaxhighlight Oforthlang="oforth">StringBuffer new "abcd" <<n(5)</langsyntaxhighlight>
 
=={{header|OpenEdge/Progress}}==
<langsyntaxhighlight Progresslang="progress (OpenEdgeopenedge ABLabl)">MESSAGE FILL( "ha", 5 ) VIEW-AS ALERT-BOX.</langsyntaxhighlight>
 
=={{header|OxygenBasic}}==
<langsyntaxhighlight lang="oxygenbasic">
 
'REPEATING A CHARACTER
Line 1,754 ⟶ 2,010:
 
print RepeatString "ABC",3 'result ABCABCABC
</syntaxhighlight>
</lang>
 
=={{header|Oz}}==
We have to write a function for this:
<langsyntaxhighlight lang="oz">declare
fun {Repeat Xs N}
if N > 0 then
Line 1,767 ⟶ 2,023:
end
in
{System.showInfo {Repeat "Ha" 5}}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
===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.
<langsyntaxhighlight lang="parigp">repeat(s,n)={
if(n, Str(repeat(s, n-1), s), "")
};</langsyntaxhighlight>
 
<code>concat()</code> joins together a vector of strings, in this case a single string repeated.
<langsyntaxhighlight lang="parigp">repeat(s,n)=concat(vector(n,i, s));</langsyntaxhighlight>
 
This solution is recursive and slightly less bad than the others for large n.
<langsyntaxhighlight lang="parigp">repeat(s,n)={
if(n<4, return(concat(vector(n,i, s))));
if(n%2,
Line 1,787 ⟶ 2,043:
repeat(Str(s,s),n\2)
);
}</langsyntaxhighlight>
 
===Version #2. Simple loop based.===
Line 1,795 ⟶ 2,051:
for the heavy text processing.
 
<langsyntaxhighlight lang="parigp">
\\ Repeat a string str the specified number of times ntimes and return composed string.
\\ 3/3/2016 aev
Line 1,816 ⟶ 2,072:
print1("6."); for(i=1,10000000, srepeat("e",10));
}
</langsyntaxhighlight>
 
{{Output}}
Line 1,835 ⟶ 2,091:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">"ha" x 5</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'*'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"ha"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,849 ⟶ 2,105:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">def rep /# s n -- s #/
"" swap
for drop
Line 1,857 ⟶ 2,113:
enddef
 
"ha" 5 rep print</langsyntaxhighlight>
Same result (simple character):
<langsyntaxhighlight Phixmontilang="phixmonti">65 5 rep
65 5 repeat
'A' 5 repeat</langsyntaxhighlight>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">str_repeat("ha", 5)</langsyntaxhighlight>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(pack (need 5 "ha"))
-> "hahahahaha"</langsyntaxhighlight>
or:
<langsyntaxhighlight PicoLisplang="picolisp">(pack (make (do 5 (link "ha"))))
-> "hahahahaha"</langsyntaxhighlight>
 
=={{header|Pike}}==
<langsyntaxhighlight lang="pike">"ha"*5;</langsyntaxhighlight>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
/* To repeat a string a variable number of times: */
 
Line 1,889 ⟶ 2,145:
 
s = (5)'h'; /* asigns 'hhhhh' to s. */
</syntaxhighlight>
</lang>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Put "ha" into a string.
Line 1,907 ⟶ 2,163:
Privatize the number.
Subtract 1 from the number.
Append the string to the original string given the number.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,915 ⟶ 2,171:
 
=={{header|Plorth}}==
<langsyntaxhighlight lang="plorth">"ha" 5 *</langsyntaxhighlight>
 
=={{header|PostScript}}==
<langsyntaxhighlight PostScriptlang="postscript">% the comments show the stack content after the line was executed
% where rcount is the repeat count, "o" is for orignal,
% "f" is for final, and iter is the for loop variable
Line 1,936 ⟶ 2,192:
} for
pop % fstring
} def</langsyntaxhighlight>
 
=={{header|PowerBASIC}}==
<langsyntaxhighlight lang="powerbasic">MSGBOX REPEAT$(5, "ha")</langsyntaxhighlight>
 
=={{header|PowerShell}}==
<langsyntaxhighlight lang="powershell">"ha" * 5 # ==> "hahahahaha"</langsyntaxhighlight>
 
=={{header|Processing}}==
<langsyntaxhighlight lang="processing">void setup() {
String rep = repeat("ha", 5);
println(rep);
Line 1,954 ⟶ 2,210:
// and return as a new String
return new String(new char[times]).replace("\0", str);
}</langsyntaxhighlight>
 
==={{header|Processing Python mode}}===
<langsyntaxhighlight lang="python">def setup():
rep = repeat("ha", 5)
println(rep)
 
def repeat(s, times):
return s * times</langsyntaxhighlight>
 
=={{header|Prolog}}==
<langsyntaxhighlight lang="prolog">%repeat(Str,Num,Res).
repeat(Str,1,Str).
repeat(Str,Num,Res):-
Num1 is Num-1,
repeat(Str,Num1,Res1),
string_concat(Str, Res1, Res).</langsyntaxhighlight>
 
=== alternative using DCG strings ===
Line 1,979 ⟶ 2,235:
{{works with|SWI-Prolog|7}}
 
<langsyntaxhighlight lang="prolog">
:- system:set_prolog_flag(double_quotes,chars) .
 
Line 2,002 ⟶ 2,258:
.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,056 ⟶ 2,312:
repeating it more than 0 times results in the concatenation of the string and (n-1) further repeats.
 
<langsyntaxhighlight lang="pure">> str_repeat 0 s = "";
> str_repeat n s = s + (str_repeat (n-1) s) if n>0;
> str_repeat 5 "ha";
"hahahahaha"
></langsyntaxhighlight>
 
You can define str_repeat using infinite lazy list (stream).
 
<langsyntaxhighlight lang="pure">
str_repeat n::int s::string = string $ take n $ cycle (s:[]);
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.s RepeatString(count, text$=" ")
Protected i, ret$=""
 
Line 2,078 ⟶ 2,334:
EndProcedure
 
Debug RepeatString(5, "ha")</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">"ha" * 5 # ==> "hahahahaha"</langsyntaxhighlight>
"Characters" are just strings of length one.
 
the other way also works:
<langsyntaxhighlight lang="python">5 * "ha" # ==> "hahahahaha"</langsyntaxhighlight>
 
=== Using a Function ===
<langsyntaxhighlight lang="python">def repeat(s, times):
return s * times
 
print(repeat("ha", 5))</langsyntaxhighlight>
{{Out}}
<pre>hahahahaha</pre>
 
=== Using Lambda ===
<langsyntaxhighlight lang="python">x = lambda a: a * 5
print(x("ha"))</langsyntaxhighlight>
{{Out}}
<pre>hahahahaha</pre>
 
=={{header|Quackery}}==
<langsyntaxhighlight Quackerylang="quackery">$ "ha" 5 of echo$</langsyntaxhighlight>
'''Output:'''
<pre>hahahahaha</pre>
 
=={{header|R}}==
<langsyntaxhighlight lang="ruby">strrep("ha", 5)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
;; fast
Line 2,116 ⟶ 2,372:
(string-append* (make-list n str)))
(string-repeat 5 "ha") ; => "hahahahaha"
</syntaxhighlight>
</lang>
 
To repeat a single character:
<langsyntaxhighlight lang="racket">
(make-string 5 #\*) => "*****"
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>print "ha" x 5</langsyntaxhighlight>
(Note that the <code>x</code> operator isn't quite the same as in Perl 5: it now only creates strings. To create lists, use <code>xx</code>.)
 
=={{header|RapidQ}}==
<syntaxhighlight lang="vb">
<lang vb>
'For a single char
showmessage String$(10, "-")
Line 2,142 ⟶ 2,398:
 
showmessage Repeat$("ha", 5)
</syntaxhighlight>
</lang>
 
=={{header|REALbasic}}==
<langsyntaxhighlight lang="vb">Function Repeat(s As String, count As Integer) As String
Dim output As String
For i As Integer = 0 To count
Line 2,152 ⟶ 2,408:
Return output
End Function
</syntaxhighlight>
</lang>
 
=={{header|REBOL}}==
<langsyntaxhighlight lang="rebol">head insert/dup "" "ha" 5</langsyntaxhighlight>
 
=={{header|Red}}==
<langsyntaxhighlight Redlang="red">>> str: "Add duplicates to string"
>> insert/dup str "ha" 3
== "hahahaAdd duplicates to string"
>> insert/dup tail str "ha" 3
== "hahahaAdd duplicates to stringhahaha"</langsyntaxhighlight>
 
=={{header|ReScript}}==
<langsyntaxhighlight ReScriptlang="rescript">Js.log(Js.String2.repeat("ha", 5))</langsyntaxhighlight>
 
=={{header|Retro}}==
<langsyntaxhighlight Retrolang="retro">with strings'
: repeatString ( $n-$ )
1- [ dup ] dip [ over prepend ] times nip ;
 
"ha" 5 repeatString</langsyntaxhighlight>
 
=={{header|REXX}}==
Since the REXX language only supports the "character" type, it's not surprising that there are so many ways to skin a cat.
<langsyntaxhighlight REXXlang="rexx">/*REXX program to show various ways to repeat a string (or repeat a single char).*/
 
/*all examples are equivalent, but not created equal.*/
Line 2,293 ⟶ 2,549:
parse value y||y||y||y||y with z
 
exit /*stick a fork in it, we're done.*/</langsyntaxhighlight>
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]].
<br><br>
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring"> Copy("ha" , 5) # ==> "hahahahaha"</langsyntaxhighlight>
 
=={{header|RPL}}==
≪ "" 1 5 START "ha" + NEXT ≫ EVAL
{{out}}
<pre>1: "hahahahaha"</pre>
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">"ha" * 5 # ==> "hahahahaha"</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">a$ = "ha "
for i = 1 to 5
a1$ = a1$ + a$
next i
a$ = a1$
print a$</langsyntaxhighlight>
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">std::iter::repeat("ha").take(5).collect::<String>(); // ==> "hahahahaha"</langsyntaxhighlight>
 
Since 1.16:
<langsyntaxhighlight lang="rust">"ha".repeat(5); // ==> "hahahahaha"</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">"ha" * 5 // ==> "hahahahaha"</langsyntaxhighlight>
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">(define (string-repeat n str)
(apply string-append (vector->list (make-vector n str))))</langsyntaxhighlight>
with SRFI 1:
<langsyntaxhighlight lang="scheme">(define (string-repeat n str)
(fold string-append "" (make-list n str)))
(string-repeat 5 "ha") ==> "hahahahaha"</langsyntaxhighlight>
 
To repeat a single character:
<langsyntaxhighlight lang="scheme">(make-string 5 #\*)</langsyntaxhighlight>
 
=={{header|Scratch}}==
Line 2,338 ⟶ 2,598:
=={{header|sed}}==
Number of ampersands indicates number of repetitions.
<langsyntaxhighlight lang="sed">
$ echo ha | sed 's/.*/&&&&&/'
hahahahaha
</syntaxhighlight>
</lang>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
begin
writeln("ha" mult 5);
end func;</langsyntaxhighlight>
 
Output:
Line 2,357 ⟶ 2,617:
 
=={{header|SenseTalk}}==
<langsyntaxhighlight lang="sensetalk">
put "Ho!" repeated 3 times
 
put "Merry" repeated to length 12
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,369 ⟶ 2,629:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">'ha' * 5; # ==> 'hahahahaha'</langsyntaxhighlight>
 
=={{header|Sinclair ZX81 BASIC}}==
Works with 1k of RAM. This program defines a subroutine that expects to find a string and a number of times to repeat it; but all it then does is loop and concatenate, so making it a separate subroutine is arguably overkill.
<langsyntaxhighlight lang="basic"> 10 LET S$="HA"
20 LET N=5
30 GOSUB 60
Line 2,382 ⟶ 2,642:
80 LET T$=T$+S$
90 NEXT I
100 RETURN</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
 
If n is a small constant, then simply concatenating n times will do; for example, n=5::
<langsyntaxhighlight lang="smalltalk">v := 'ha'.
v,v,v,v,v</langsyntaxhighlight>
 
{{works with|Pharo|1.4}}
Line 2,395 ⟶ 2,655:
By creating a collection of n 'ha', and joining them to a string:
 
<langsyntaxhighlight lang="smalltalk">((1 to: n) collect: [:x | 'ha']) joinUsing: ''.</langsyntaxhighlight>
or:{{works with|Smalltalk/X}}
{{works with|VisualWorks Smalltalk}}
 
<langsyntaxhighlight lang="smalltalk">(Array new:n withAll:'ha') asStringWith:''.</langsyntaxhighlight>
By creating a WriteStream, and putting N times the string 'ha' into it:
 
<langsyntaxhighlight lang="smalltalk">ws := '' writeStream.
n timesRepeat: [ws nextPutAll: 'ha'].
ws contents.</langsyntaxhighlight>
alternatively:
<langsyntaxhighlight lang="smalltalk">(String streamContents:[:ws | n timesRepeat: [ws nextPutAll: 'ha']])</langsyntaxhighlight>
 
all evaluate to:
Line 2,414 ⟶ 2,674:
 
A string containing a repeated character is generated with:
<syntaxhighlight lang ="smalltalk">String new:n withAll:$*</langsyntaxhighlight>
 
{{works with|VA Smalltalk}}
<langsyntaxhighlight lang="smalltalk">(String new:n) atAllPut:$*</langsyntaxhighlight>
 
=={{header|SNOBOL4}}==
<langsyntaxhighlight lang="snobol4"> output = dupl("ha",5)
end</langsyntaxhighlight>
 
=={{header|Sparkling}}==
<langsyntaxhighlight lang="sparkling">spn:3> repeat("na", 8) .. " Batman!"
= nananananananana Batman!</langsyntaxhighlight>
 
=={{header|SQL}}==
<langsyntaxhighlight lang="sql">select rpad('', 10, 'ha')</langsyntaxhighlight>
 
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
<langsyntaxhighlight lang="sql pl">
VALUES REPEAT('ha', 5);
VALUES RPAD('', 10, 'ha');
</syntaxhighlight>
</lang>
Output:
<pre>
Line 2,457 ⟶ 2,717:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="sml">fun string_repeat (s, n) =
concat (List.tabulate (n, fn _ => s))
;</langsyntaxhighlight>
 
testing in the interpreter:
<langsyntaxhighlight lang="sml">- string_repeat ("Hiuoa", 3) ;
val it = "HiuoaHiuoaHiuoa" : string</langsyntaxhighlight>
 
To repeat a single character:
<langsyntaxhighlight lang="sml">fun char_repeat (c, n) =
implode (List.tabulate (n, fn _ => c))
;</langsyntaxhighlight>
 
=={{header|Stata}}==
<langsyntaxhighlight lang="stata">. scalar a="ha"
. scalar b=a*5
. display b
hahahahaha</langsyntaxhighlight>
 
=={{header|Suneido}}==
<langsyntaxhighlight Suneidolang="suneido">'ha'.Repeat(5) --> "hahahahaha"
'*'.Repeat(5) --> "*****"</langsyntaxhighlight>
 
=={{header|Swift}}==
Line 2,484 ⟶ 2,744:
=== The Builtin Way ===
 
<langsyntaxhighlight lang="swift">print(String(repeating:"*", count: 5))</langsyntaxhighlight>
{{out}}*****
 
=== Functions ===
 
<langsyntaxhighlight lang="swift">func * (left:String, right:Int) -> String {
return String(repeating:left, count:right)
}
 
print ("HA" * 5)
</syntaxhighlight>
</lang>
{{out}}
HAHAHAHAHA
Line 2,502 ⟶ 2,762:
Using extensions to do the repetition which makes for an easier syntax when repeating Strings, and using String.extend() to get faster evaluation.
 
<langsyntaxhighlight lang="swift">extension String {
// Slower version
func repeatString(n: Int) -> String {
Line 2,520 ⟶ 2,780:
 
print( "ha".repeatString(5) )
print( "he".repeatString2(5) )</langsyntaxhighlight>
{{out}}
<pre>
Line 2,528 ⟶ 2,788:
 
To repeat a single character:
<langsyntaxhighlight lang="swift">String(count:5, repeatedValue:"*" as Character)
</syntaxhighlight>
</lang>
 
Note that using the String version on a string of 1 Character, or the repeat single Character version is timewise close to the same. No point in using the Character version for efficiency (tested with repeating up to 100 000 times).
Line 2,536 ⟶ 2,796:
The following version is an enhanced version of the [http://rosettacode.org/mw/index.php?title=Repeat_a_string#Recursive_version recursive ActionScript], where we're using bit operation along with iterative doubling of the string to get to the correctly repeated version of the text in the most effective manner without recursion. When benchmarked against the plain iterative version in previous section, this version is marginally better, but only my a very small percentage. The critical factor for making the repeat function effective when using larger strings (1000 characters) and multiple repeats (1000 repeats :-) ) was to to exchange the '+=' with 'String.extend' method.
 
<langsyntaxhighlight lang="swift">extension String {
func repeatBiterative(count: Int) -> String {
var reduceCount = count
Line 2,554 ⟶ 2,814:
}
 
"He".repeatBiterative(5)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,561 ⟶ 2,821:
 
=={{header|Tailspin}}==
<langsyntaxhighlight lang="tailspin">
'$:1..5 -> 'ha';' -> !OUT::write
</syntaxhighlight>
</lang>
{{out}}
<pre>hahahahaha</pre>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">string repeat "ha" 5 ;# => hahahahaha</langsyntaxhighlight>
 
=={{header|TorqueScript}}==
--[[User:Eepos|Eepos]]
<langsyntaxhighlight TorqueScriptlang="torquescript">function strRep(%str,%int)
{
for(%i = 0; %i < %int; %i++)
Line 2,580 ⟶ 2,840:
 
return %rstr;
}</langsyntaxhighlight>
 
=={{header|Tosh}}==
<langsyntaxhighlight Toshlang="tosh">when flag clicked
set String to "meow"
set Count to 4
Line 2,590 ⟶ 2,850:
set Repeated to (join (Repeated) (String))
end
stop this script</langsyntaxhighlight>
 
=={{header|Transact-SQL}}==
<langsyntaxhighlight lang="tsql">select REPLICATE( 'ha', 5 )</langsyntaxhighlight>
 
=={{header|TUSCRIPT}}==
<langsyntaxhighlight lang="tuscript">
$$ MODE TUSCRIPT
repeatstring=REPEAT ("ha",5)
</syntaxhighlight>
</lang>
 
=={{header|UNIX Shell}}==
Line 2,606 ⟶ 2,866:
{{works with|ksh93}}
{{works with|zsh}}
<langsyntaxhighlight lang="bash">printf "ha"%.0s {1..5}</langsyntaxhighlight>
 
With ksh93 and zsh, the count can vary.
Line 2,612 ⟶ 2,872:
{{works with|ksh93}}
{{works with|zsh}}
<langsyntaxhighlight lang="bash">i=5
printf "ha"%.0s {1..$i}</langsyntaxhighlight>
 
With bash, <code>{1..$i}</code> fails, because brace expansion happens before variable substitution. The fix uses <code>eval</code>.
Line 2,620 ⟶ 2,880:
{{works with|ksh93}}
{{works with|zsh}}
<langsyntaxhighlight lang="bash">i=5
eval "printf 'ha'%.0s {1..$i}"</langsyntaxhighlight>
 
For the general case, one must escape any % or \ characters in the string, because <code>printf</code> would interpret those characters.
Line 2,628 ⟶ 2,888:
{{works with|ksh93}}
{{works with|zsh}}
<langsyntaxhighlight lang="bash">reprint() {
typeset e="$(sed -e 's,%,%%,g' -e 's,\\,\\\\,g' <<<"$1")"
eval 'printf "$e"%.0s '"{1..$2}"
}
reprint '% ha \' 5</langsyntaxhighlight>
 
=== Using repeat ===
Line 2,638 ⟶ 2,898:
{{works with|csh}}
 
<langsyntaxhighlight lang="bash">
len=12; str='='
repeat $len printf "$str"
</syntaxhighlight>
</lang>
 
===Using head -c===
Line 2,648 ⟶ 2,908:
{{works with|Bourne Shell}}
 
<langsyntaxhighlight lang="sh">width=72; char='='
head -c ${width} < /dev/zero | tr '\0' "$char"</langsyntaxhighlight>
 
=={{header|Ursala}}==
<langsyntaxhighlight Ursalalang="ursala">#import nat
 
repeat = ^|DlSL/~& iota
Line 2,658 ⟶ 2,918:
#cast %s
 
example = repeat('ha',5)</langsyntaxhighlight>
output:
<pre>'hahahahaha'</pre>
Line 2,664 ⟶ 2,924:
=={{header|Vala}}==
Repeat a string 5 times:
<langsyntaxhighlight lang="vala">
string s = "ha";
string copy = "";
for (int x = 0; x < 5; x++)
copy += s;
</syntaxhighlight>
</lang>
 
Fill a string with a char N times:
<langsyntaxhighlight lang="vala">
string s = string.nfill(5, 'c');
</syntaxhighlight>
</lang>
 
=={{header|VBA}}==
<br>'''Repeat a string'''<br>
<langsyntaxhighlight VBAlang="vba">Public Function RepeatStr(aString As String, aNumber As Integer) As String
Dim bString As String, i As Integer
bString = ""
Line 2,687 ⟶ 2,947:
End Function
 
Debug.Print RepeatStr("ha", 5)</langsyntaxhighlight>
{{out}}
<pre>
Line 2,694 ⟶ 2,954:
''Note:'' "String(5, "ha") in VBA produces "hhhhh" (only the first character is repeated)!
<p>An alternative method:
<langsyntaxhighlight lang="vba">Public Function RepeatString(stText As String, iQty As Integer) As String
RepeatString = Replace(String(iQty, "x"), "x", stText)
End Function</langsyntaxhighlight>
<br>'''Repeat a character'''<br>
<langsyntaxhighlight VBAlang="vba">Debug.Print String(5, "x")</langsyntaxhighlight>
{{out}}
<pre>xxxxx</pre>
Line 2,704 ⟶ 2,964:
=={{header|VBScript}}==
{{works with|Windows Script Host|*}}
<syntaxhighlight lang="vbscript">
<lang VBScript>
' VBScript has a String() function that can repeat a character a given number of times
' but this only works with single characters (or the 1st char of a string):
Line 2,712 ⟶ 2,972:
WScript.Echo Replace(Space(10), " ", "Ha")
WScript.Echo Replace(String(10, "X"), "X", "Ha")
</syntaxhighlight>
</lang>
 
=={{header|Vedit macro language}}==
<langsyntaxhighlight lang="vedit">Ins_Text("ha", COUNT, 5) </langsyntaxhighlight>
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
<br>'''Repeat a string'''<br>
<langsyntaxhighlight lang="vb">Public Function StrRepeat(s As String, n As Integer) As String
Dim r As String, i As Integer
r = ""
Line 2,729 ⟶ 2,989:
End Function
Debug.Print StrRepeat("ha", 5)</langsyntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
 
An alternative method:
<langsyntaxhighlight lang="vb">Public Function StrRepeat(sText As String, n As Integer) As String
StrRepeat = Replace(String(n, "*"), "*", sText)
End Function</langsyntaxhighlight>
 
<br>'''Repeat a character'''<br>
<langsyntaxhighlight VBAlang="vba">Debug.Print String(5, "x")</langsyntaxhighlight>
{{out}}
<pre>xxxxx</pre>
Line 2,745 ⟶ 3,005:
=={{header|Visual Basic .NET}}==
<br>'''Repeat a string'''<br>
<syntaxhighlight lang="vb">
<lang vb>
Debug.Print(Replace(Space(5), " ", "Ha"))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,754 ⟶ 3,014:
 
<br>'''Repeat a character'''<br>
<syntaxhighlight lang="vb">
<lang vb>
Debug.Print(StrDup(5, "x"))
Debug.Print("".PadRight(5, "x"))
Debug.Print("".PadLeft(5, "x"))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,768 ⟶ 3,028:
=={{header|Visual FoxPro}}==
Use the built in function REPLICATE(string, number):
<langsyntaxhighlight lang="vfp">? REPLICATE("HO", 3)</langsyntaxhighlight>
 
produces
<pre>HOHOHO</pre>
 
=={{header|V (Vlang)}}==
<langsyntaxhighlight lang="go">// Repeat a string, in V
// Tectonics: v run repeat-a-string.v
module main
Line 2,787 ⟶ 3,047:
// This indexes the string to get the first byte of the rune array
println(strings.repeat("*"[0], 5))
}</langsyntaxhighlight>
{{out}}
<pre>prompt$ v run rosetta/repeat-a-string.v
hahahahaha
*****</pre>
 
=={{header|VBScript}}==
 
<syntaxhighlight lang="vb">
replace(string(5,"@"),"@","hello")
</syntaxhighlight>
{{out}}
<small>
<pre>
hellohellohellohellohello
</pre>
</small>
 
=={{header|Wart}}==
<langsyntaxhighlight lang="wart">def (s * n) :case (string? s)
with outstring
repeat n
Line 2,800 ⟶ 3,072:
 
("ha" * 5)
=> "hahahahaha"</langsyntaxhighlight>
 
=={{header|Wortel}}==
<langsyntaxhighlight lang="wortel">@join "" @rep 5 "ha" ; returns "hahahahaha"</langsyntaxhighlight>
As a function:
<langsyntaxhighlight lang="wortel">^(\@join "" @rep)</langsyntaxhighlight>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">System.print("ha" * 5)</langsyntaxhighlight>
 
{{out}}
Line 2,816 ⟶ 3,088:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">cod T=12; int I; for I gets 1,5 do T(0,"ha")</langsyntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
 
=={{header|Yorick}}==
<langsyntaxhighlight lang="yorick">array("ha", 5)(sum)</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
<langsyntaxhighlight lang="z80">PrintChar equ &BB5A ;Amstrad CPC BIOS call, prints the ascii code in the accumulator to the screen.
 
org &8000
Line 2,848 ⟶ 3,120:
call PrintChar ; BIOS call, all regs are preserved.
inc hl ; next char
jr PrintString ; back to start.</langsyntaxhighlight>
 
{{out}}
Line 2,857 ⟶ 3,129:
=={{header|zig}}==
At compile-time:
<langsyntaxhighlight lang="zig">const laugh = "ha" ** 5;</langsyntaxhighlight>
Note that to achieve this at run-time in zig (in order to avoid hidden overflows) you must manage the memory yourself.
<langsyntaxhighlight lang="zig">const std = @import("std");
const warn = std.debug.warn;
 
Line 2,879 ⟶ 3,151:
const ex = try repeat("ha", 5, allocator);
defer allocator.free(ex);
}</langsyntaxhighlight>
 
=={{header|zkl}}==
Same as [[#Ruby|Ruby]]
<langsyntaxhighlight lang="zkl">"ha" * 5 # --> "hahahahaha"</langsyntaxhighlight>
 
 
885

edits