Repeat a string: Difference between revisions

Content added Content deleted
Line 1,108: Line 1,108:
=={{header|Smalltalk}}==
=={{header|Smalltalk}}==


Simply concatenating 5 times will do:
If n is a small constant, then simply concatenating n times will do; for example, n=5::
<lang smalltalk>v := 'ha'.
<lang smalltalk>v := 'ha'.
v,v,v,v,v</lang>
v,v,v,v,v</lang>
Line 1,115: Line 1,115:
{{works with|Smalltalk/X}}
{{works with|Smalltalk/X}}


By creating a collection of 5 'ha', and joining them to a string:
By creating a collection of n 'ha', and joining them to a string:


<lang smalltalk>((1 to: 5) collect: [:x | 'ha']) joinUsing: ''.</lang>
<lang smalltalk>((1 to: n) collect: [:x | 'ha']) joinUsing: ''.</lang>
or:{{works with|Smalltalk/X}}
or:{{works with|Smalltalk/X}}
{{works with|VisualWorks Smalltalk}}
<lang smalltalk>(Array new:5 withAll:'ha') asStringWith:''.</lang>

By creating a WriteStream, and putting 5 times the string 'ha' into it:
<lang smalltalk>(Array new:n withAll:'ha') asStringWith:''.</lang>
By creating a WriteStream, and putting N times the string 'ha' into it:


<lang smalltalk>ws := '' writeStream.
<lang smalltalk>ws := '' writeStream.
5 timesRepeat: [ws nextPutAll: 'ha'].
n timesRepeat: [ws nextPutAll: 'ha'].
ws contents.</lang>
ws contents.</lang>
alternatively:
alternatively:
<lang smalltalk>(String streamContents:[:ws | 5 timesRepeat: [ws nextPutAll: 'ha']])</lang>
<lang smalltalk>(String streamContents:[:ws | n timesRepeat: [ws nextPutAll: 'ha']])</lang>


Evaluates to:
all evaluate to:
<pre>
<pre>
hahahahaha
hahahahaha