Repeat a string: Difference between revisions

Line 1,108:
=={{header|Smalltalk}}==
 
SimplyIf n is a small constant, then simply concatenating 5n times will do; for example, n=5::
<lang smalltalk>v := 'ha'.
v,v,v,v,v</lang>
Line 1,115:
{{works with|Smalltalk/X}}
 
By creating a collection of 5n 'ha', and joining them to a string:
 
<lang smalltalk>((1 to: 5n) collect: [:x | 'ha']) joinUsing: ''.</lang>
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:5n withAll:'ha') asStringWith:''.</lang>
By creating a WriteStream, and putting 5N times the string 'ha' into it:
 
<lang smalltalk>ws := '' writeStream.
5n timesRepeat: [ws nextPutAll: 'ha'].
ws contents.</lang>
alternatively:
<lang smalltalk>(String streamContents:[:ws | 5n timesRepeat: [ws nextPutAll: 'ha']])</lang>
 
Evaluatesall evaluate to:
<pre>
hahahahaha
Anonymous user