Repeat a string: Difference between revisions
Content added Content deleted
Line 1,108: | Line 1,108: | ||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
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 |
By creating a collection of n 'ha', and joining them to a string: |
||
<lang smalltalk>((1 to: |
<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>ws := '' writeStream. |
<lang smalltalk>ws := '' writeStream. |
||
n timesRepeat: [ws nextPutAll: 'ha']. |
|||
ws contents.</lang> |
ws contents.</lang> |
||
alternatively: |
alternatively: |
||
<lang smalltalk>(String streamContents:[:ws | |
<lang smalltalk>(String streamContents:[:ws | n timesRepeat: [ws nextPutAll: 'ha']])</lang> |
||
all evaluate to: |
|||
<pre> |
<pre> |
||
hahahahaha |
hahahahaha |