Best way to replicate a string?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Best way to replicate a string?

Costas Menico
What is the best way to replicate a string n number of times?

I was hoping to find a message in the String class such as in the
example:

str := 'abc' repeat: 4

Costas


Reply | Threaded
Open this post in threaded view
|

Re: Best way to replicate a string?

Ian Bartholomew
Costas,

"Costas Menico" <[hidden email]> wrote in message
news:[hidden email]...
> What is the best way to replicate a string n number of times?
>
> I was hoping to find a message in the String class such as in the
> example:
>
> str := 'abc' repeat: 4

I don't think there is anything existing in the image to do this but it's
easy enough to add a method to String -

String>>repeat: anInteger
    | stream |
    stream := String writeStream.
    anInteger timesRepeat: [stream nextPutAll: self].
    ^stream contents

Ian