Repeat a string: Difference between revisions
→{{header|langur}}
Langurmonkey (talk | contribs) |
|||
(56 intermediate revisions by 36 users not shown) | |||
Line 10:
{{Template:Strings}}
<br><br>
=={{header|11l}}==
<syntaxhighlight lang="11l">print(‘ha’ * 5)</syntaxhighlight>
{{out}}
<pre>
hahahahaha
</pre>
=={{header|360 Assembly}}==
<
REPEATS CSECT
USING REPEATS,R13 base register
Line 54 ⟶ 62:
PG DC CL80' ' pg
REGEQU
END REPEATS </
{{out}}
<pre>
Line 62 ⟶ 70:
=={{header|4DOS Batch}}==
<
echo %@repeat[*,5]
quit
Line 71 ⟶ 79:
enddo
echo.
return</
Output shows:
<pre>hahahahaha
*****</pre>
=={{header|6502 Assembly}}==
<syntaxhighlight lang="6502asm">CHROUT equ $FFD2 ;KERNAL call, prints the accumulator to the screen as an ascii value.
org $0801
db $0E,$08,$0A,$00,$9E,$20,$28,$32,$30,$36,$34,$29,$00,$00,$00
lda #>TestStr
sta $11
lda #<TestStr
sta $10
ldx #5 ;number of times to repeat
loop:
jsr PrintString
dex
bne loop
RTS ;RETURN TO BASIC
PrintString:
ldy #0
loop_PrintString:
lda ($10),y ;this doesn't actually increment the pointer itself, so we don't need to back it up.
beq donePrinting
jsr CHROUT
iny
jmp loop_PrintString
donePrinting:
rts
TestStr:
db "HA",0</syntaxhighlight>
{{out}}
<pre>
READY.
LOAD"*",8,1:
SEARCHING FOR *
LOADING
READY.
RUN
HAHAHAHAHA
READY.
</pre>
=={{header|68000 Assembly}}==
Easiest way to do this is with a loop.
<syntaxhighlight lang="68000devpac">MOVE.W #5-1,D1
RepString:
LEA A3, MyString
MOVE.L A3,-(SP) ;PUSH A3
JSR PrintString ;unimplemented hardware-dependent printing routine, assumed to not clobber D1
MOVE.L (SP)+,A3 ;POP A3
DBRA D1,RepString
RTS ;return to basic or whatever
MyString:
DC.B "ha",0
even</syntaxhighlight>
=={{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}}==
<
. cr</
Output shows:
<pre>hahahahaha</pre>
Line 85 ⟶ 200:
This works for ABAP Version 7.40 and above
<syntaxhighlight lang="abap">
report z_repeat_string.
write repeat( val = `ha` occ = 5 ).
</syntaxhighlight>
{{out}}
<pre>
hahahahaha
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">Proc Main()
byte REPEAT
REPEAT=5
Do
Print("ha")
REPEAT==-1
Until REPEAT=0
Do
Return</syntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
=={{header|ActionScript}}==
Line 103 ⟶ 232:
===Iterative version===
<
{
var output:String = "";
Line 109 ⟶ 238:
output += string;
return output;
}</
===Recursive version===
The following double-and-add method is much faster when repeating a string many times.
<
{
if(numTimes == 0) return "";
Line 119 ⟶ 248:
var tmp:String = repeatRecursive(string, numTimes/2);
return tmp + tmp;
}</
===Flex===
<
trace(StringUtil.repeat("ha", 5));
</syntaxhighlight>
Sample Output:
<pre>
Line 132 ⟶ 261:
=={{header|Ada}}==
In [[Ada]] multiplication of an universal integer to string gives the desired result. Here is an example of use:
<
with Ada.Text_IO; use Ada.Text_IO;
Line 138 ⟶ 267:
begin
Put_Line (5 * "ha");
end String_Multiplication;</
Sample output:
<pre>
Line 145 ⟶ 274:
=={{header|Aime}}==
<
=={{header|ALGOL 68}}==
<
</syntaxhighlight>
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="amazing hopper">
#!/usr/bin/hopper
#include <hopper.h>
main:
{"ha"}replyby(5), println
{"ha",5}replicate, println
{0}return
</syntaxhighlight>
hahahahaha
hahahahaha
=={{header|APL}}==
Fill up a string of length 10 with 'ha':
<
hahahahaha</
Alternatively, define a function:
<
5 REPEAT 'ha'
hahahahaha</
=={{header|AppleScript}}==
<
set final_string to ""
repeat 5 times
set final_string to final_string & str
end repeat</
Line 173 ⟶ 315:
{{trans|JavaScript}}
<
-- Repetition by 'Egyptian multiplication' -
Line 192 ⟶ 334:
end repeat
return out & dbl
end replicate</
=={{header|Applesoft BASIC}}==
<
? "X" SPC(20) "X"</
Output:
<pre>X X</pre>
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">print repeat "ha" 5</syntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
=={{header|AutoHotkey}}==
<
Repeat(String,Times)
Line 254 ⟶ 358:
Output .= String
Return Output
}</
=={{header|AutoIt}}==
<
ConsoleWrite(_StringRepeat("ha", 5) & @CRLF)</
=={{header|AWK}}==
<
{
for( ; i<n; i++ )
Line 271 ⟶ 375:
BEGIN {
print repeat( "ha", 5 )
}</
=={{header|Babel}}==
<
print_repeat!: { <- { dup << } -> times }</
Outputs:
<syntaxhighlight lang
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:
<
s$ = s$ & "ha"
DONE
PRINT s$</
{{out}}
<pre>
Line 292 ⟶ 396:
</pre>
To repeat one single character:
<
{{out}}
<pre>
xxxxx
</pre>
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="basic256">function StringRepeat$ (s$, n)
cad$ = ""
for i = 1 to n
cad$ += s$
next i
return cad$
end function
print StringRepeat$("rosetta", 1)
print StringRepeat$("ha", 5)
print StringRepeat$("*", 5)
end</syntaxhighlight>
==={{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}}===
<syntaxhighlight lang="qbasic">FUNCTION StringRepeat$ (s$, n)
cad$ = ""
FOR i = 1 TO n
cad$ = cad$ + s$
NEXT i
StringRepeat$ = cad$
END FUNCTION
PRINT StringRepeat$("rosetta", 1)
PRINT StringRepeat$("ha", 5)
PRINT StringRepeat$("*", 5)
END</syntaxhighlight>
==={{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}}===
<syntaxhighlight lang="qbasic">FUNCTION StringRepeat$ (s$, n)
LET cad$ = ""
FOR i = 1 TO n
LET cad$ = cad$ & s$
NEXT i
LET StringRepeat$ = cad$
END FUNCTION
PRINT StringRepeat$("rosetta", 1)
PRINT StringRepeat$("ha", 5)
PRINT StringRepeat$("*", 5)
END</syntaxhighlight>
==={{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}}===
<syntaxhighlight lang="yabasic">sub StringRepeat$ (s$, n)
cad$ = ""
for i = 1 to n
cad$ = cad$ + s$
next i
return cad$
end sub
print StringRepeat$("rosetta", 1)
print StringRepeat$("ha", 5)
print StringRepeat$("*", 5)
end</syntaxhighlight>
=={{header|Batch File}}==
Commandline implementation
<
if "%2" equ "" goto fail
setlocal enabledelayedexpansion
Line 307 ⟶ 587:
for /l %%i in (1,1,%num%) do set res=!res!%char%
echo %res%
:fail</
'Function' version
<
set /p a=Enter string to repeat :
set /p b=Enter how many times to repeat :
Line 319 ⟶ 599:
set "c=%c%+=1"
if /i _"%c%"==_"%d%" (exit /b)
goto :a</
'Function' version 2
<
@FOR /L %%i in (0,1,9) DO @CALL :REPEAT %%i
@echo That's it!
Line 334 ⟶ 614:
@GOTO:EOF
:END</
=={{header|BBC BASIC}}==
<
=={{header|beeswax}}==
<
p0~1<}~< d@<
_VT@1~>yg~9PKd@M'd;</
Line 354 ⟶ 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}}==
<
>29*+00p>~:"0"- #v_v $
v ^p0p00:-1g00< $ >
v p00&p0-1g00+4*65< >00g1-:00p#^_@</
Input sample:
<pre>ha05</pre>
Line 366 ⟶ 661:
Output sample:
<pre>hahahahaha</pre>
=={{header|BQN}}==
<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.
<syntaxhighlight lang="bqn">Repeat ← ×⟜≠ ⥊ ⊢
•Show 5 Repeat "Hello"</syntaxhighlight><syntaxhighlight lang="text">"HelloHelloHelloHelloHello"</syntaxhighlight>
=={{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.
<
string N rep
. !arg:(?string.?N)
Line 377 ⟶ 680:
' (!N+-1:>0:?N&!string !rep:?rep)
& str$!rep
);</
<pre> repeat$(ha.5)
Line 384 ⟶ 687:
=={{header|Brainf***}}==
Prints "ha" 10 times. Note that this method only works for a number of repetitions that fit into the cell size.
<
[-> +++++ +++++<] we add 10 to second each loopround
Line 392 ⟶ 695:
and a newline because I'm kind and it looks good
+++++ +++++ +++ . --- .</
=={{header|Brat}}==
<
=={{header|Burlesque}}==
<
blsq ) 'h5?*
"hhhhh"
blsq ) "ha"5.*\[
"hahahahaha"
</syntaxhighlight>
=={{header|C}}==
<
#include <stdlib.h>
#include <string.h>
Line 427 ⟶ 730:
free(result);
return 0;
}</
A variation.
<
char *string_repeat(const char *str, int n)
{
Line 441 ⟶ 744:
while (pa>=dest) *pa-- = *pb--;
return dest;
}</
To repeat a single character
<
#include <stdlib.h>
#include <string.h>
Line 460 ⟶ 763:
free(result);
return 0;
}</
If you use [[GLib]], simply use <code>g_strnfill ( gsize length, gchar fill_char )</code> function.
=={{header|C sharp}}==
<
or (with .NET 2+)
<
or (with .NET 2+)
<
or (with .NET 4+)
<
To repeat a single character:
<
or (with .NET 2+)
<
=={{header|C++}}==
<
#include <iostream>
Line 493 ⟶ 796:
std::cout << repeat( "Ha" , 5 ) << std::endl ;
return 0 ;
}</
To repeat a single character:
<
#include <iostream>
Line 502 ⟶ 805:
std::cout << std::string( 5, '*' ) << std::endl ;
return 0 ;
}</
=== recursive version ===
<
#include <iostream>
Line 520 ⟶ 823:
std::cout << repeat( "Ha" , 5 ) << std::endl ;
return 0 ;
}</
=={{header|Ceylon}}==
<
print("ha".repeat(5));
}</
=={{header|Clipper}}==
Also works with Harbour Project compiler Harbour 3.0.0 (Rev. 16951)
<
=={{header|Clojure}}==
<
=={{header|COBOL}}==
Virtually a one-liner.
<
PROGRAM-ID. REPEAT-PROGRAM.
DATA DIVISION.
Line 544 ⟶ 847:
MOVE ALL 'ha' TO HAHA.
DISPLAY HAHA.
STOP RUN.</
{{out}}
<pre>hahahahaha</pre>
=={{header|ColdFusion}}==
<
<cfset word = 'ha'>
<Cfset n = 5>
Line 555 ⟶ 858:
<Cfloop from="1" to="#n#" index="i">#word#</Cfloop>
</Cfoutput>
</syntaxhighlight>
=={{header|Common Lisp}}==
<
(with-output-to-string (stream)
(loop repeat n do (write-string string stream))))</
A version which allocates the result string in one step:
<
&aux
(len (length string))
Line 572 ⟶ 875:
for i from 0 by len
do (setf (subseq result i (+ i len)) string))
result)</
For those who love one-liners, even at the expense of readability:
<
(format nil "~V@{~a~:*~}" n string))</
<
A single character may be repeated using just the builtin <code>make-string</code>:
<
produces “XXXXX”.
=={{header|Crystal}}==
<syntaxhighlight lang="ruby">
puts "ha" * 5
</syntaxhighlight>
<pre>hahahahaha</pre>
=={{header|D}}==
Repeating a string:
<
void main() {
writeln("ha".replicate(5));
}</
Repeating a character with vector operations:
<
void main() {
Line 611 ⟶ 912:
chars[] = '*'; // set all characters in the string to '*'
writeln(chars);
}</
=={{header|DCL}}==
Not exactly what the task asks for but at least it is something;
<
$ write sys$output f$fao( "!12*d" )</
{{out}}
<pre>$ @repeat_a_string_and_then_character
Line 624 ⟶ 925:
=={{header|Delphi}}==
Repeat a string
<syntaxhighlight lang="delphi">
function RepeatString(const s: string; count: cardinal): string;
var
Line 634 ⟶ 935:
Writeln(RepeatString('ha',5));
</syntaxhighlight>
Repeat a character
<syntaxhighlight lang="delphi">
Writeln( StringOfChar('a',5) );
</syntaxhighlight>
Using recursion
<syntaxhighlight lang="delphi">
function RepeatStr(const s: string; i: Cardinal): string;
begin
Line 652 ⟶ 953:
result := s + RepeatStr(s, i-1)
end;
</syntaxhighlight>
Built in RTL function:
<syntaxhighlight lang
=={{header|DWScript}}==
Repeat a string
<syntaxhighlight lang="delphi">
PrintLn( StringOfString('abc',5) );
</syntaxhighlight>
Repeat a character
<syntaxhighlight lang="delphi">
PrintLn( StringOfChar('a',5) );
</syntaxhighlight>
=={{header|Dyalect}}==
<syntaxhighlight lang
=={{header|Déjà Vu}}==
<
{{out}}
<pre>"hahahahaha"</pre>
=={{header|E}}==
<
=={{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'</
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 699 ⟶ 1,011:
RepeatString('ha',3);
RepeatString('Who',2);</
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
String funny = "ha" * 5;
String stars = '*' * 80;
</syntaxhighlight>
=={{header|Egison}}==
<
(S.concat (take 5 (repeat1 "ha")))
</syntaxhighlight>
=={{header|Eiffel}}==
<
repeat_string(a_string: STRING; times: INTEGER): STRING
require
Line 714 ⟶ 1,032:
Result := a_string.multiply(times)
end
</syntaxhighlight>
=={{header|Elena}}==
ELENA
<
import extensions;
import extensions'text;
Line 724 ⟶ 1,042:
public program()
{
var s := new Range(0, 5).selectBy::(x => "ha").summarize(new StringWriter())
}</
=={{header|Elixir}}==
<
String.duplicate("ha", 5)
</syntaxhighlight>
=={{header|Emacs Lisp}}==
Going via a list to repeat the desired string
<
A single character can be repeated with <code>make-string</code>:
<syntaxhighlight lang
{{libheader|cl-lib}}
<syntaxhighlight lang="lisp">(require 'cl-lib)
(cl-loop repeat 5 concat "ha")</syntaxhighlight>
=={{header|Erlang}}==
<
lists:flatten(lists:duplicate(N,X)).</
This will duplicate a string or character N times to produce a new string.
=={{header|ERRE}}==
<syntaxhighlight lang="erre">
PROCEDURE REPEAT_STRING(S$,N%->REP$)
LOCAL I%
Line 760 ⟶ 1,079:
END FOR
END PROCEDURE
</syntaxhighlight>
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 767 ⟶ 1,086:
=={{header|Euphoria}}==
A simple loop will do:
<syntaxhighlight lang="euphoria">
sequence s = ""
for i = 1 to 5 do s &= "ha" end for
Line 773 ⟶ 1,092:
hahahahaha
</syntaxhighlight>
For repeating a single character:
<syntaxhighlight lang="euphoria">
sequence s = repeat('*',5)
*****
</syntaxhighlight>
For repeating a string or sequence of numbers:
<syntaxhighlight lang="euphoria">
include std/console.e -- for display
include std/sequence.e -- for repeat_pattern
Line 793 ⟶ 1,112:
hahahahaha
{1,2,3,1,2,3,1,2,3,1,2,3,1,2,3}
</syntaxhighlight>
But wait, here's another way:
<syntaxhighlight lang="euphoria">
include std/console.e -- for display
include std/sequence.e -- for flatten
sequence s = flatten(repeat("ha",5))
display(s)
</syntaxhighlight>
note: repeat creates a sequence of ha's as shown below; flatten concatenates them.
<syntaxhighlight lang="euphoria">
{
"ha",
Line 812 ⟶ 1,131:
"ha"
}
</syntaxhighlight>
=={{header|Explore}}==
Line 820 ⟶ 1,139:
=={{header|F_Sharp|F#}}==
<
val it : string = "hahahahaha"</
Or
<
val it : string = "hahahahaha"</
=={{header|Factor}}==
<
"ha" 5 repeat-string print</
=={{header|Forth}}==
<
0 dest c!
n 0 ?do src len dest +place loop ;
s" ha" pad 5 place-n
pad count type \ hahahahaha</
The same code without the use of locals:
<
: place-n ( src len dest n -- )
swap >r 0 r@ c!
Line 846 ⟶ 1,165:
s" ha" pad 5 place-n
pad count type \ hahahahaha</
Filling a string with a single character is supported by ANS-Forth:
<
pad 10 type \ **********</
=={{header|Fortran}}==
{{works with |Fortran|90 and later}}
<
write (*, '(a)') repeat ('ha', 5)
end program test_repeat</
Output:
hahahahaha
=={{header|Free Pascal}}==
<
Repetition of a single character:
<syntaxhighlight lang
If the repeated character happens to be the space character:
<syntaxhighlight lang
=={{header|FreeBASIC}}==
<
' A character is essentially a string of length 1 in FB though there is a built-in function, String,
Line 897 ⟶ 1,216:
Print
Print "Press any key to quit program"
Sleep</
{{out}}
Line 907 ⟶ 1,226:
=={{header|Frink}}==
<
println[repeat["ha", 5]]
</syntaxhighlight>
=={{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</
Output = hahahahaha
=={{header|GAP}}==
<
"BOB BOB BOB BOB BOB BOB BOB BOB BOB BOB "</
=={{header|Glee}}==
<syntaxhighlight lang
<
Str# => Len;
1..Len %% (Len * 5) => Idx;
Str [Idx] $;</
<
=={{header|Go}}==
<
There is no special way to repeat a single character, other than to convert the character to a string. The following works:
<
=={{header|Groovy}}==
<syntaxhighlight lang
=={{header|Harbour}}==
<
=={{header|Haskell}}==
For a string of finite length:
<
Or with list-monad (a bit obscure):
<
Or with Control.Applicative:
<
For an infinitely long string:
<
To repeat a single character:
<syntaxhighlight lang
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''':
<
repString s n =
let rep x = xs
Line 971 ⟶ 1,308:
main :: IO ()
main = print $ repString "ha" 5</
{{Out}}
<pre>"hahahahaha"</pre>
Line 977 ⟶ 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):
<
import Data.List (unfoldr)
import Control.Monad (join)
Line 1,001 ⟶ 1,338:
-- TEST -----------------------------------------------------------------------
main :: IO ()
main = print $ repString 500 "ha"</
=={{header|HicEst}}==
<
EDIT(Text=out, Insert="ha", DO=5)</
=={{header|Icon}} and {{header|Unicon}}==
The procedure <tt>repl</tt> is a supplied function in Icon and Unicon.
<
write(repl(integer(!args) | 5))
end</
If it weren't, one way to write it is:
<
every (ns := "") ||:= |s\(0 <= n)
return ns
end</
=={{header|Idris}}==
<
strRepeat Z s = ""
strRepeat (S n) s = s ++ strRepeat n s
Line 1,026 ⟶ 1,363:
chrRepeat : Nat -> Char -> String
chrRepeat Z c = ""
chrRepeat (S n) c = strCons c $ chrRepeat n c</
=={{header|Inform 7}}==
<
To decide which indexed text is (T - indexed text) repeated (N - number) times:
Line 1,039 ⟶ 1,376:
When play begins:
say "ha" repeated 5 times;
end the story.</
=={{Header|Insitux}}==
<syntaxhighlight lang="insitux">
(str* "ha" 5)
</syntaxhighlight>
{{out}}
<pre>
hahahahaha
</pre>
=={{header|IS-BASIC}}==
<
100 DEF STRING$(S$,N)
105 LET ST$=""
Line 1,049 ⟶ 1,398:
130 NEXT
140 LET STRING$=ST$
150 END DEF</
=={{header|J}}==
<
*****
5 # 'ha' NB. repeat each item 5 times
Line 1,058 ⟶ 1,407:
5 ((* #) $ ]) 'ha' NB. repeat array 5 times
hahahahaha
5 ;@# < 'ha' NB. using boxing
hahahahaha</
=={{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+}}
<
StringBuilder sb = new StringBuilder(str.length() * times);
for (int i = 0; i < times; i++)
Line 1,075 ⟶ 1,447:
public static void main(String[] args) {
System.out.println(repeat("ha", 5));
}</
Or even shorter:
<
return new String(new char[times]).replace("\0", str);
}</
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,088 ⟶ 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.
<
return new Array(1 + (n || 0)).join(this);
}
console.log("ha".repeat(5)); // hahahahaha</
As of ES6, `repeat` is built in, so this can be written as:
<
console.log("ha".repeat(5)); // hahahahaha</
====Repetition by Egyptian multiplication====
Line 1,104 ⟶ 1,476:
See the technique of 'Egyptian Multiplication' described in the Rhind Mathematical Papyrus at the British Museum.
<
'use strict';
Line 1,122 ⟶ 1,494:
return replicate(5000, "ha")
})();</
====Concat . replicate====
Or, more generically, we could derive '''repeat''' as the composition of '''concat''' and '''replicate'''
<
'use strict';
Line 1,152 ⟶ 1,524:
// TEST -------------------------------------------------------------------
return repeat(5, 'ha');
})();</
{{Out}}
<pre>hahahahaha</pre>
=={{header|
<syntaxhighlight lang="joy">DEFINE repeat == "" rotate [concat] cons times.
"ha" 5 repeat.</syntaxhighlight>
=={{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}}
<
# The ^ operator is really just call to the `repeat` function
@show repeat("ha", 5)</
=={{header|K}}==
<syntaxhighlight lang="k">
,/5#,"ha"
"hahahahaha"
Line 1,177 ⟶ 1,553:
5#"*"
"*****"
</syntaxhighlight>
=={{header|Kotlin}}==
<
println("ha".repeat(5))
}</
Or more fancy:
<
fun main(args: Array<String>) = println("ha" * 5)</
=={{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.
[[file:LabVIEW_Repeat_a_string.png]]
<br/>
By using built in functions:
[[File:Panel.png]]
[[File:BlockDiagram.png]]
<br/>
=={{header|Lambdatalk}}==
<
{S.map {lambda {_} ha} {S.serie 1 10}}
-> ha ha ha ha ha ha ha ha ha ha
Line 1,216 ⟶ 1,598:
{repeat ha 10}
-> hahahahahahahahahahaha
</syntaxhighlight>
=={{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>
=={{header|Lasso}}==
<syntaxhighlight lang
<
=={{header|LFE}}==
<
(string:copies '"ha" 5)
</syntaxhighlight>
=={{header|Liberty BASIC}}==
<
print StringRepeat$( a$, 5)
Line 1,244 ⟶ 1,640:
next i
StringRepeat$ =o$
end function</
=={{header|Lingo}}==
*Take a string and repeat it some number of times.
<
res = ""
repeat with i = 1 to n
Line 1,254 ⟶ 1,650:
end repeat
return res
end</
<
-- "hahahahaha"</
*If there is a simpler/more efficient way to repeat a single “character”...
<
-- "*****"</
=={{header|LiveCode}}==
<
put repeatString("ha", 5)
end mouseUp
Line 1,271 ⟶ 1,667:
end repeat
return t
end repeatString</
=={{header|Logo}}==
<
if :n = 0 [output :acc]
output (copies :n-1 :thing combine :acc :thing)
end</
or using cascade:
<
Lhogho doesn't have cascade (yet), nor does it have the initialise a missing parameter capability demonstrated by the [:acc "||] above.
<
if :n = 0 [output :acc]
output (copies :n-1 :thing combine :acc :thing)
end
print copies 5 "ha "||</
=={{header|Lua}}==
<
Or use native string library function
<syntaxhighlight lang
=={{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">
> use StringTools in
> Repeat( "abc", 10 ); # repeat an arbitrary string
Line 1,306 ⟶ 1,702:
"xxxxxxxxxxxxxxxxxxxx"
</syntaxhighlight>
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">
> cat( "abc" $ 10 );
"abcabcabcabcabcabcabcabcabcabc"
Line 1,314 ⟶ 1,710:
> cat( seq( "abc", i = 1 .. 10 ) );
"abcabcabcabcabcabcabcabcabcabc"
</syntaxhighlight>
You ''can'' build up a string in a loop, but this is highly inefficient (quadratic); don't do this.
<syntaxhighlight lang="maple">
> s := "":
> to 10 do s := cat( s, "abc" ) end: s;
"abcabcabcabcabcabcabcabcabcabc"
</syntaxhighlight>
If you need to build up a string incrementally, use a StringBuffer object, which keeps things linear.
Finally, note that strings and characters are not distinct datatypes in Maple; a character is just a string of length one.
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">StringRepeat["ha", 5]</syntaxhighlight>
=={{header|MATLAB}} / {{header|Octave}}==
<
S = repmat(s , [1,n]) ;
return</
Note 1: The repetition is returned, not displayed.
Line 1,345 ⟶ 1,734:
=={{header|Maxima}}==
<
infix("$*")$
"abc" $* 5;
/* "abcabcabcabcabc" */</
=={{header|Mercury}}==
Mercury's 'string' module provides an efficient char-repeater. The following uses string.builder to repeat strings.
<
:- interface.
:- import_module string, char, int.
Line 1,379 ⟶ 1,768:
print(Stream, String, !S),
printn(Stream, N - 1, String, !S)
; true ).</
=={{header|min}}==
{{works with|min|0.19.3}}
<
{{out}}
<pre>
Line 1,390 ⟶ 1,779:
=={{header|MiniScript}}==
<
print str * 5</
{{out}}
<pre>
Line 1,398 ⟶ 1,787:
=={{header|Mirah}}==
<
5.times do
Line 1,404 ⟶ 1,793:
end
puts x # ==> "hahahahaha"</
=={{header|Monte}}==
<syntaxhighlight lang="monte">
var s := "ha " * 5
traceln(s)
</syntaxhighlight>
=={{header|MontiLang}}==
<syntaxhighlight lang
Or with a loop
<
|ha| OUT .
ENDFOR || PRINT .</
Or ...
<
=={{header|MUMPS}}==
<
;Repeat a string S for N times
NEW I
Line 1,433 ⟶ 1,822:
F I=1:1:N W S
Q
</syntaxhighlight>
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">
;Even better (more terse)
S x="",$P(x,"-",10)="-"
W x
</syntaxhighlight>
=={{header|Nanoquery}}==
In Nanoquery, multiplying strings by an integer returns a new string with the original value repeated.
<
=={{header|Neko}}==
<
var srep = function(s, n) {
var str = ""
Line 1,458 ⟶ 1,847:
}
$print(srep("ha", 5), "\n")</
{{out}}
Line 1,467 ⟶ 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).
<
using System.Console;
Line 1,484 ⟶ 1,873:
}
}</
=={{header|NetRexx}}==
NetRexx has built in functions to manipulate strings. The most appropriate for this task is the <code>'''copies()'''</code> function:
<
ha5 = 'ha'.copies(5)
</syntaxhighlight>
There are several other built-in functions that can be used to achieve the same result depending on need:
<
sampleStr = 'ha' -- string to duplicate
say ' COPIES:' sampleStr.copies(5)
Line 1,507 ⟶ 1,896:
say ' SUBSTR:' ''.substr(1, 5, sampleChr)
say 'TRANSLATE:' '.....'.translate(sampleChr, '.')
</syntaxhighlight>
=={{header|NewLISP}}==
<
=={{header|Nim}}==
<syntaxhighlight lang="nim">import strutils
# Repeat a char.
echo repeat('a', 5) # -> "aaaaa".
# Repeat a string.
echo repeat("ha", 5) # -> "hahahahaha".</syntaxhighlight>
=={{header|Objeck}}==
<
class Repeat {
function : Main(args : String[]) ~ Nil {
Line 1,534 ⟶ 1,926:
}
}
}</
=={{header|Objective-C}}==
Line 1,543 ⟶ 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.
<
- (NSString *) repeatStringByNumberOfTimes: (NSUInteger) times;
@end
Line 1,551 ⟶ 1,943:
return [@"" stringByPaddingToLength:[self length]*times withString:self startingAtIndex:0];
}
@end</
Now, let's put it to use:
<
// -repeatByNumberOfTimes: selector.
NSString *aString = [@"ha" repeatStringByNumberOfTimes:5];
// Display the NSString.
NSLog(@"%@", aString);</
=={{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.
<
let s = Bytes.of_string s in
let len = Bytes.length s in
let res = Bytes.create (n * len) in
for i = 0 to pred n do
Bytes.blit s 0 res (i * len) len
done;
(Bytes.to_string res
;;</
which gives the signature<
testing in the toplevel:
<
- : string = "HiuoaHiuoaHiuoa"</
Alternately create an array initialized to s, and concat:
<
String.concat "" (Array.to_list (Array.make n s))
;;</
Or:
<
Array.fold_left (^) "" (Array.make n s)
;;</
To repeat a single character use:
<syntaxhighlight lang
=={{header|Oforth}}==
<
=={{header|OpenEdge/Progress}}==
<
=={{header|OxygenBasic}}==
<
'REPEATING A CHARACTER
Line 1,617 ⟶ 2,010:
print RepeatString "ABC",3 'result ABCABCABC
</syntaxhighlight>
=={{header|Oz}}==
We have to write a function for this:
<
fun {Repeat Xs N}
if N > 0 then
Line 1,630 ⟶ 2,023:
end
in
{System.showInfo {Repeat "Ha" 5}}</
=={{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.
<
if(n, Str(repeat(s, n-1), s), "")
};</
<code>concat()</code> joins together a vector of strings, in this case a single string repeated.
<
This solution is recursive and slightly less bad than the others for large n.
<
if(n<4, return(concat(vector(n,i, s))));
if(n%2,
Line 1,650 ⟶ 2,043:
repeat(Str(s,s),n\2)
);
}</
===Version #2. Simple loop based.===
Line 1,658 ⟶ 2,051:
for the heavy text processing.
<
\\ Repeat a string str the specified number of times ntimes and return composed string.
\\ 3/3/2016 aev
Line 1,679 ⟶ 2,072:
print1("6."); for(i=1,10000000, srepeat("e",10));
}
</
{{Output}}
Line 1,698 ⟶ 2,091:
=={{header|Perl}}==
<
=={{header|Phix}}==
<!--<syntaxhighlight lang="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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,710 ⟶ 2,105:
=={{header|Phixmonti}}==
<
"" swap
for drop
Line 1,718 ⟶ 2,113:
enddef
"ha" 5 rep print</
Same result (simple character):
<
65 5 repeat
'A' 5 repeat</
=={{header|PHP}}==
<
=={{header|PicoLisp}}==
<
-> "hahahahaha"</
or:
<
-> "hahahahaha"</
=={{header|Pike}}==
<
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
/* To repeat a string a variable number of times: */
Line 1,750 ⟶ 2,145:
s = (5)'h'; /* asigns 'hhhhh' to s. */
</syntaxhighlight>
=={{header|Plain English}}==
<
Start up.
Put "ha" into a string.
Line 1,768 ⟶ 2,163:
Privatize the number.
Subtract 1 from the number.
Append the string to the original string given the number.</
{{out}}
<pre>
Line 1,776 ⟶ 2,171:
=={{header|Plorth}}==
<
=={{header|PostScript}}==
<
% where rcount is the repeat count, "o" is for orignal,
% "f" is for final, and iter is the for loop variable
Line 1,797 ⟶ 2,192:
} for
pop % fstring
} def</
=={{header|PowerBASIC}}==
<
=={{header|PowerShell}}==
<
=={{header|Processing}}==
<
String rep = repeat("ha", 5);
println(rep);
Line 1,815 ⟶ 2,210:
// and return as a new String
return new String(new char[times]).replace("\0", str);
}</
==={{header|Processing Python mode}}===
<
rep = repeat("ha", 5)
println(rep)
def repeat(s, times):
return s * times</
=={{header|Prolog}}==
<
repeat(Str,1,Str).
repeat(Str,Num,Res):-
Num1 is Num-1,
repeat(Str,Num1,Res1),
string_concat(Str, Res1, Res).</
=== alternative using DCG strings ===
Line 1,848 ⟶ 2,235:
{{works with|SWI-Prolog|7}}
<
:- system:set_prolog_flag(double_quotes,chars) .
Line 1,871 ⟶ 2,258:
.
</syntaxhighlight>
{{out}}
Line 1,925 ⟶ 2,312:
repeating it more than 0 times results in the concatenation of the string and (n-1) further repeats.
<
> str_repeat n s = s + (str_repeat (n-1) s) if n>0;
> str_repeat 5 "ha";
"hahahahaha"
></
You can define str_repeat using infinite lazy list (stream).
<
str_repeat n::int s::string = string $ take n $ cycle (s:[]);
</syntaxhighlight>
=={{header|PureBasic}}==
<
Protected i, ret$=""
Line 1,947 ⟶ 2,334:
EndProcedure
Debug RepeatString(5, "ha")</
=={{header|Python}}==
<
"Characters" are just strings of length one.
the other way also works:
<
=== Using a Function ===
<syntaxhighlight lang="python">def repeat(s, times):
return s * times
print(repeat("ha", 5))</syntaxhighlight>
{{Out}}
<pre>hahahahaha</pre>
=== Using Lambda ===
<syntaxhighlight lang="python">x = lambda a: a * 5
print(x("ha"))</syntaxhighlight>
{{Out}}
<pre>hahahahaha</pre>
=={{header|Quackery}}==
<syntaxhighlight lang="quackery">$ "ha" 5 of echo$</syntaxhighlight>
'''Output:'''
<pre>hahahahaha</pre>
=={{header|R}}==
<
=={{header|Racket}}==
<
#lang racket
;; fast
Line 1,966 ⟶ 2,372:
(string-append* (make-list n str)))
(string-repeat 5 "ha") ; => "hahahahaha"
</syntaxhighlight>
To repeat a single character:
<
(make-string 5 #\*) => "*****"
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
(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">
'For a single char
showmessage String$(10, "-")
Line 1,992 ⟶ 2,398:
showmessage Repeat$("ha", 5)
</syntaxhighlight>
=={{header|REALbasic}}==
<
Dim output As String
For i As Integer = 0 To count
Line 2,002 ⟶ 2,408:
Return output
End Function
</syntaxhighlight>
=={{header|REBOL}}==
<
=={{header|Red}}==
<
>> insert/dup str "ha" 3
== "hahahaAdd duplicates to string"
>> insert/dup tail str "ha" 3
== "hahahaAdd duplicates to stringhahaha"</
=={{header|ReScript}}==
<syntaxhighlight lang="rescript">Js.log(Js.String2.repeat("ha", 5))</syntaxhighlight>
=={{header|Retro}}==
<
: repeatString ( $n-$ )
1- [ dup ] dip [ over prepend ] times nip ;
"ha" 5 repeatString</
=={{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.
<
/*all examples are equivalent, but not created equal.*/
Line 2,140 ⟶ 2,549:
parse value y||y||y||y||y with z
exit /*stick a fork in it, we're done.*/</
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]].
<br><br>
=={{header|Ring}}==
<
=={{header|RPL}}==
≪ "" 1 5 START "ha" + NEXT ≫ EVAL
{{out}}
<pre>1: "hahahahaha"</pre>
=={{header|Ruby}}==
<
=={{header|Run BASIC}}==
<
for i = 1 to 5
a1$ = a1$ + a$
next i
a$ = a1$
print a$</
=={{header|Rust}}==
<
Since 1.16:
<
=={{header|Scala}}==
<
=={{header|Scheme}}==
<
(apply string-append (vector->list (make-vector n str))))</
with SRFI 1:
<
(fold string-append "" (make-list n str)))
(string-repeat 5 "ha") ==> "hahahahaha"</
To repeat a single character:
<
=={{header|Scratch}}==
Line 2,185 ⟶ 2,598:
=={{header|sed}}==
Number of ampersands indicates number of repetitions.
<
$ echo ha | sed 's/.*/&&&&&/'
hahahahaha
</syntaxhighlight>
=={{header|Seed7}}==
<
const proc: main is func
begin
writeln("ha" mult 5);
end func;</
Output:
Line 2,204 ⟶ 2,617:
=={{header|SenseTalk}}==
<
put "Ho!" repeated 3 times
put "Merry" repeated to length 12
</syntaxhighlight>
Output:
<pre>
Line 2,216 ⟶ 2,629:
=={{header|Sidef}}==
<
=={{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.
<
20 LET N=5
30 GOSUB 60
Line 2,229 ⟶ 2,642:
80 LET T$=T$+S$
90 NEXT I
100 RETURN</
=={{header|Smalltalk}}==
If n is a small constant, then simply concatenating n times will do; for example, n=5::
<
v,v,v,v,v</
{{works with|Pharo|1.4}}
Line 2,242 ⟶ 2,655:
By creating a collection of n 'ha', and joining them to a string:
<
or:{{works with|Smalltalk/X}}
{{works with|VisualWorks Smalltalk}}
<
By creating a WriteStream, and putting N times the string 'ha' into it:
<
n timesRepeat: [ws nextPutAll: 'ha'].
ws contents.</
alternatively:
<
all evaluate to:
Line 2,261 ⟶ 2,674:
A string containing a repeated character is generated with:
<syntaxhighlight lang
{{works with|VA Smalltalk}}
<
=={{header|SNOBOL4}}==
<
end</
=={{header|Sparkling}}==
<
= nananananananana Batman!</
=={{header|SQL}}==
<
=={{header|SQL PL}}==
{{works with|Db2 LUW}}
<
VALUES REPEAT('ha', 5);
VALUES RPAD('', 10, 'ha');
</syntaxhighlight>
Output:
<pre>
Line 2,304 ⟶ 2,717:
=={{header|Standard ML}}==
<
concat (List.tabulate (n, fn _ => s))
;</
testing in the interpreter:
<
val it = "HiuoaHiuoaHiuoa" : string</
To repeat a single character:
<
implode (List.tabulate (n, fn _ => c))
;</
=={{header|Stata}}==
<
. scalar b=a*5
. display b
hahahahaha</
=={{header|Suneido}}==
<
'*'.Repeat(5) --> "*****"</
=={{header|Swift}}==
Line 2,331 ⟶ 2,744:
=== The Builtin Way ===
<
{{out}}*****
=== Functions ===
<
return String(repeating:left, count:right)
}
print ("HA" * 5)
</syntaxhighlight>
{{out}}
HAHAHAHAHA
Line 2,349 ⟶ 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.
<
// Slower version
func repeatString(n: Int) -> String {
Line 2,367 ⟶ 2,780:
print( "ha".repeatString(5) )
print( "he".repeatString2(5) )</
{{out}}
<pre>
Line 2,375 ⟶ 2,788:
To repeat a single character:
<
</syntaxhighlight>
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,383 ⟶ 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.
<
func repeatBiterative(count: Int) -> String {
var reduceCount = count
Line 2,401 ⟶ 2,814:
}
"He".repeatBiterative(5)</
{{out}}
<pre>
Line 2,408 ⟶ 2,821:
=={{header|Tailspin}}==
<
'$:1..5 -> 'ha';' -> !OUT::write
</syntaxhighlight>
{{out}}
<pre>hahahahaha</pre>
=={{header|Tcl}}==
<
=={{header|TorqueScript}}==
--[[User:Eepos|Eepos]]
<
{
for(%i = 0; %i < %int; %i++)
Line 2,427 ⟶ 2,840:
return %rstr;
}</
=={{header|Tosh}}==
<
set String to "meow"
set Count to 4
Line 2,437 ⟶ 2,850:
set Repeated to (join (Repeated) (String))
end
stop this script</
=={{header|Transact-SQL}}==
<
=={{header|TUSCRIPT}}==
<
$$ MODE TUSCRIPT
repeatstring=REPEAT ("ha",5)
</syntaxhighlight>
=={{header|UNIX Shell}}==
Line 2,453 ⟶ 2,866:
{{works with|ksh93}}
{{works with|zsh}}
<
With ksh93 and zsh, the count can vary.
Line 2,459 ⟶ 2,872:
{{works with|ksh93}}
{{works with|zsh}}
<
printf "ha"%.0s {1..$i}</
With bash, <code>{1..$i}</code> fails, because brace expansion happens before variable substitution. The fix uses <code>eval</code>.
Line 2,467 ⟶ 2,880:
{{works with|ksh93}}
{{works with|zsh}}
<
eval "printf 'ha'%.0s {1..$i}"</
For the general case, one must escape any % or \ characters in the string, because <code>printf</code> would interpret those characters.
Line 2,475 ⟶ 2,888:
{{works with|ksh93}}
{{works with|zsh}}
<
typeset e="$(sed -e 's,%,%%,g' -e 's,\\,\\\\,g' <<<"$1")"
eval 'printf "$e"%.0s '"{1..$2}"
}
reprint '% ha \' 5</
=== Using repeat ===
Line 2,485 ⟶ 2,898:
{{works with|csh}}
<
len=12; str='='
repeat $len printf "$str"
</syntaxhighlight>
===Using head -c===
Line 2,495 ⟶ 2,908:
{{works with|Bourne Shell}}
<
head -c ${width} < /dev/zero | tr '\0' "$char"</
=={{header|Ursala}}==
<
repeat = ^|DlSL/~& iota
Line 2,505 ⟶ 2,918:
#cast %s
example = repeat('ha',5)</
output:
<pre>'hahahahaha'</pre>
Line 2,511 ⟶ 2,924:
=={{header|Vala}}==
Repeat a string 5 times:
<
string s = "ha";
string copy = "";
for (int x = 0; x < 5; x++)
copy += s;
</syntaxhighlight>
Fill a string with a char N times:
<
string s = string.nfill(5, 'c');
</syntaxhighlight>
=={{header|VBA}}==
<br>'''Repeat a string'''<br>
<
Dim bString As String, i As Integer
bString = ""
Line 2,534 ⟶ 2,947:
End Function
Debug.Print RepeatStr("ha", 5)</
{{out}}
<pre>
Line 2,541 ⟶ 2,954:
''Note:'' "String(5, "ha") in VBA produces "hhhhh" (only the first character is repeated)!
<p>An alternative method:
<
RepeatString = Replace(String(iQty, "x"), "x", stText)
End Function</
<br>'''Repeat a character'''<br>
<
{{out}}
<pre>xxxxx</pre>
Line 2,551 ⟶ 2,964:
=={{header|VBScript}}==
{{works with|Windows Script Host|*}}
<syntaxhighlight 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,559 ⟶ 2,972:
WScript.Echo Replace(Space(10), " ", "Ha")
WScript.Echo Replace(String(10, "X"), "X", "Ha")
</syntaxhighlight>
=={{header|Vedit macro language}}==
<
=={{header|Visual Basic}}==
{{works with|Visual Basic|VB6 Standard}}
<br>'''Repeat a string'''<br>
<
Dim r As String, i As Integer
r = ""
Line 2,576 ⟶ 2,989:
End Function
Debug.Print StrRepeat("ha", 5)</
{{out}}
<pre>hahahahaha</pre>
An alternative method:
<
StrRepeat = Replace(String(n, "*"), "*", sText)
End Function</
<br>'''Repeat a character'''<br>
<
{{out}}
<pre>xxxxx</pre>
Line 2,592 ⟶ 3,005:
=={{header|Visual Basic .NET}}==
<br>'''Repeat a string'''<br>
<syntaxhighlight lang="vb">
Debug.Print(Replace(Space(5), " ", "Ha"))
</syntaxhighlight>
{{out}}
<pre>
Line 2,601 ⟶ 3,014:
<br>'''Repeat a character'''<br>
<syntaxhighlight lang="vb">
Debug.Print(StrDup(5, "x"))
Debug.Print("".PadRight(5, "x"))
Debug.Print("".PadLeft(5, "x"))
</syntaxhighlight>
{{out}}
<pre>
Line 2,615 ⟶ 3,028:
=={{header|Visual FoxPro}}==
Use the built in function REPLICATE(string, number):
<
produces
<pre>HOHOHO</pre>
=={{header|V (Vlang)}}==
<syntaxhighlight lang="go">// Repeat a string, in V
// Tectonics: v run repeat-a-string.v
module main
import strings
// starts here
pub fn main() {
// A strings module function to repeat strings
println(strings.repeat_string("ha", 5))
// Another strings module function to repeat a byte
// This indexes the string to get the first byte of the rune array
println(strings.repeat("*"[0], 5))
}</syntaxhighlight>
{{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}}==
<
with outstring
repeat n
Line 2,627 ⟶ 3,072:
("ha" * 5)
=> "hahahahaha"</
=={{header|Wortel}}==
<
As a function:
<
=={{header|Wren}}==
<
{{out}}
Line 2,643 ⟶ 3,088:
=={{header|XPL0}}==
<
{{out}}
<pre>hahahahaha</pre>
=={{header|Yorick}}==
<
=={{header|Z80 Assembly}}==
<syntaxhighlight lang="z80">PrintChar equ &BB5A ;Amstrad CPC BIOS call, prints the ascii code in the accumulator to the screen.
org &8000
ld b,5 ; repeat 5 times
loop:
call PrintImmediate
byte "ha",0
djnz loop
ret ; return to basic
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
PrintImmediate:
pop hl ; get the return address into HL, it's the start of the embedded string.
call PrintString
; inc hl ; if your strings are null-terminated you can omit this, since a 0 equals the "NOP" instruction
jp (hl) ; acts as a ret, returning execution to the instruction just after the embedded string.
PrintString:
ld a,(hl) ; read in a character from the string
or a ; if your strings are null-terminated you can use this as a shortcut, otherwise use the compare instruction
ret z ; exit once the terminator is reached.
call PrintChar ; BIOS call, all regs are preserved.
inc hl ; next char
jr PrintString ; back to start.</syntaxhighlight>
{{out}}
<pre>
hahahahaha
</pre>
=={{header|zig}}==
At compile-time:
<
Note that to achieve this at run-time in zig (in order to avoid hidden overflows) you must manage the memory yourself.
<
const warn = std.debug.warn;
Line 2,674 ⟶ 3,151:
const ex = try repeat("ha", 5, allocator);
defer allocator.free(ex);
}</
=={{header|zkl}}==
Same as [[#Ruby|Ruby]]
<
|